custom/plugins/FourtwosixPhoneValidation/src/Subscriber/AddCountryPhonePrefixSubscriber.php line 133

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixPhoneValidation\Subscriber;
  3. use FourtwosixPhoneValidation\Service\Routes\CustomerAddressRoute;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. use Shopware\Core\System\Country\CountryEntity;
  12. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  13. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  14. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class AddCountryPhonePrefixSubscriber implements EventSubscriberInterface
  19. {
  20.     protected EntityRepositoryInterface $countryRepository;
  21.     protected EntityRepositoryInterface $customerAddressRepository;
  22.     protected CustomerAddressRoute $customerAddressRoute;
  23.     /**
  24.      * @param EntityRepositoryInterface $countryRepository
  25.      * @param EntityRepositoryInterface $customerAddressRepository
  26.      * @param CustomerAddressRoute      $customerAddressRoute
  27.      */
  28.     public function __construct(
  29.         EntityRepositoryInterface $countryRepository,
  30.         EntityRepositoryInterface $customerAddressRepository,
  31.         CustomerAddressRoute      $customerAddressRoute
  32.     ) {
  33.         $this->countryRepository $countryRepository;
  34.         $this->customerAddressRepository $customerAddressRepository;
  35.         $this->customerAddressRoute $customerAddressRoute;
  36.     }
  37.     /**
  38.      * @return string[]
  39.      */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoadedEvent',
  44.             AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoadedEvent',
  45.             AddressDetailPageLoadedEvent::class => 'onAddressDetailPageLoadedEvent',
  46.             AddressListingPageLoadedEvent::class => 'onAddressListingPageLoadedEvent',
  47.         ];
  48.     }
  49.     /**
  50.      * @param       $event
  51.      * @param array $ftsPRefixes
  52.      * @return void
  53.      */
  54.     protected function addCountryExtension($event, array $ftsPRefixes = []): void
  55.     {
  56.         $countries = [];
  57.         // We retrieve only country with custom field set sorted by ISO code (which will be prepended in twig)
  58.         $criteria = new Criteria();
  59.         $criteria->addSorting(new FieldSorting('iso'));
  60.         $criteria->addFilter(
  61.             new NotFilter(
  62.                 MultiFilter::CONNECTION_AND,
  63.                 [
  64.                     new EqualsFilter('customFields.fourtwosix_phone_country_prefix'null),
  65.                 ]
  66.             )
  67.         );
  68.         $countryEntities $this->countryRepository->search($criteria$event->getContext())->getEntities();
  69.         /** @var CountryEntity $countryEntity */
  70.         foreach ($countryEntities as $countryEntity) {
  71.             $countryCustomFields $countryEntity->getTranslated()["customFields"] ?? null;
  72.             if (is_null($countryCustomFields)) {
  73.                 continue;
  74.             }
  75.             $prefixes $countryCustomFields["fourtwosix_phone_country_prefix"] ?? null;
  76.             if (is_null($prefixes)) {
  77.                 continue;
  78.             }
  79.             // In cases like '+39,' we do not want ['+39', '']
  80.             $explodedePrefixes array_filter(explode(","$prefixes));
  81.             // For cases like '+421, ' we do not want ['+421', ' ']
  82.             $trimmedPrefixes array_filter(
  83.                 array_map(function ($prefix) {
  84.                     return trim($prefix);
  85.                 }, $explodedePrefixes)
  86.             );
  87.             // add each prefix in a separate line to use just a plain for loop in twig
  88.             foreach ($trimmedPrefixes as $trimmedPrefix) {
  89.                 $countries[] = [
  90.                     "iso" => $countryEntity->getIso(),
  91.                     "prefix" => $trimmedPrefix,
  92.                 ];
  93.             }
  94.         }
  95.         $data = [
  96.             "countries" => $countries,
  97.             "fts_prefixes" => $ftsPRefixes,
  98.         ];
  99.         $event->getPage()->addExtension(
  100.             "FourtwosixPhoneNumberCountryPrefix",
  101.             new ArrayStruct($data)
  102.         );
  103.     }
  104.     /**
  105.      * Checkout registration -> does not need to load customer
  106.      * @param CheckoutRegisterPageLoadedEvent $event
  107.      * @return void
  108.      */
  109.     public function onCheckoutRegisterPageLoadedEvent(CheckoutRegisterPageLoadedEvent $event): void
  110.     {
  111.         $this->addCountryExtension($event);
  112.     }
  113.     /**
  114.      * Login/Registration -> does not need to load customer
  115.      * @param AccountLoginPageLoadedEvent $event
  116.      * @return void
  117.      */
  118.     public function onAccountLoginPageLoadedEvent(AccountLoginPageLoadedEvent $event): void
  119.     {
  120.         $this->addCountryExtension($event);
  121.     }
  122.     /**
  123.      * Address created/updated from page not AJAX like in Profile Overview or in checkout/confirm
  124.      * @param AddressDetailPageLoadedEvent $event
  125.      * @return void
  126.      */
  127.     public function onAddressDetailPageLoadedEvent(AddressDetailPageLoadedEvent $event): void
  128.     {
  129.         $address $event->getPage()->getAddress();
  130.         $salesChannelContext $event->getSalesChannelContext();
  131.         $context $salesChannelContext->getContext();
  132.         if (is_null($address)) {
  133.             // Create new address -> fetch data from customer
  134.             $customer $salesChannelContext->getCustomer();
  135.             if (is_null($customer)) {
  136.                 return;
  137.             }
  138.             $customerId $customer->getId();
  139.             $ftsPrefixes $this->customerAddressRoute->getCustomerAddressPrefixFromCustomer($customerId$context);
  140.         } else {
  141.             // Edit address -> fetch data from customerAddressId
  142.             $addressid $address->getId();
  143.             $ftsPrefixes $this->customerAddressRoute->getCustomerAddressPrefixFromAddressId($addressid$context);
  144.         }
  145.         $this->addCountryExtension($event$ftsPrefixes);
  146.     }
  147.     /**
  148.      * @param AddressListingPageLoadedEvent $event
  149.      * @return void
  150.      */
  151.     public function onAddressListingPageLoadedEvent(AddressListingPageLoadedEvent $event): void
  152.     {
  153.         $salesChannelContext $event->getSalesChannelContext();
  154.         $customerId $salesChannelContext->getCustomer()->id;
  155.         $context $salesChannelContext->getContext();
  156.         // Load customerAddressPrefixes
  157.         $ftsPrefixes $this->customerAddressRoute->getCustomerAddressPrefixFromCustomer($customerId$context);
  158.         $this->addCountryExtension($event$ftsPrefixes);
  159.     }
  160. }