vendor/shopware/storefront/Controller/CountryStateController.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  4. use Shopware\Core\Framework\Feature;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\Framework\Routing\Annotation\Since;
  7. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  10. use Shopware\Storefront\Pagelet\Country\CountryStateDataPageletLoadedHook;
  11. use Shopware\Storefront\Pagelet\Country\CountryStateDataPageletLoader;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Route(defaults={"_routeScope"={"storefront"}})
  18.  *
  19.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  20.  */
  21. #[Package('system-settings')]
  22. class CountryStateController extends StorefrontController
  23. {
  24.     private CountryStateDataPageletLoader $countryStateDataPageletLoader;
  25.     /**
  26.      * @deprecated tag:v6.5.0 - $countryRoute will be removed
  27.      */
  28.     private AbstractCountryRoute $countryRoute;
  29.     /**
  30.      * @internal
  31.      */
  32.     public function __construct(
  33.         CountryStateDataPageletLoader $countryStateDataPageletLoader,
  34.         AbstractCountryRoute $countryRoute
  35.     ) {
  36.         $this->countryStateDataPageletLoader $countryStateDataPageletLoader;
  37.         $this->countryRoute $countryRoute;
  38.     }
  39.     /**
  40.      * @Since("6.1.0.0")
  41.      * This route should only be used by storefront to update address forms. It is not a replacement for store-api routes
  42.      *
  43.      * @HttpCache()
  44.      * @Route("country/country-state-data", name="frontend.country.country.data", defaults={"csrf_protected"=false, "XmlHttpRequest"=true}, methods={ "POST" })
  45.      */
  46.     public function getCountryData(Request $requestSalesChannelContext $context): Response
  47.     {
  48.         $countryId = (string) $request->request->get('countryId');
  49.         if (!$countryId) {
  50.             throw new \InvalidArgumentException('Parameter countryId is empty');
  51.         }
  52.         $countryStateDataPagelet $this->countryStateDataPageletLoader->load($countryId$request$context);
  53.         $this->hook(new CountryStateDataPageletLoadedHook($countryStateDataPagelet$context));
  54.         /** @deprecated tag:v6.5.0 - stateRequired will be removed - remove complete if branch */
  55.         if (!Feature::isActive('v6.5.0.0')) {
  56.             $stateRequired false;
  57.             $countries $this->countryRoute->load(
  58.                 new Request(),
  59.                 new Criteria([$countryId]),
  60.                 $context
  61.             )->getCountries();
  62.             if ($countries->first() !== null) {
  63.                 $stateRequired $countries->first()->getForceStateInRegistration();
  64.             }
  65.             return new JsonResponse([
  66.                 'stateRequired' => $stateRequired/** @deprecated tag:v6.5.0 - stateRequired will be removed */
  67.                 'states' => $countryStateDataPagelet->getStates(),
  68.             ]);
  69.         }
  70.         return new JsonResponse([
  71.             'states' => $countryStateDataPagelet->getStates(),
  72.         ]);
  73.     }
  74. }