vendor/store.shopware.com/tonurpackstation6/src/Subscriber/PackstationSubscriber.php line 147

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tonur\Packstation\Subscriber;
  3. use Psr\Log\LoggerAwareTrait;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressCollection;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\System\Country\CountryCollection;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Event\StorefrontRenderEvent;
  13. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  14. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  15. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Shopware\Core\Checkout\Customer\CustomerEvents;
  19. use Tonur\Packstation\Checkout\Packstation\PackstationEntity;
  20. use Tonur\Packstation\Service\PackstationService;
  21. use Tonur\Packstation\TonurPackstation6;
  22. class PackstationSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var SystemConfigService
  26.      */
  27.     private $systemConfigService;
  28.     /**
  29.      * @var PackstationService
  30.      */
  31.     private $packstationService;
  32.     /**
  33.      * @var EntityRepositoryInterface
  34.      */
  35.     private $countryRepository;
  36.     use LoggerAwareTrait;
  37.     /**
  38.      * @param SystemConfigService $systemConfigService
  39.      * @param PackstationService $packstationService
  40.      * @param EntityRepositoryInterface $countryRepository
  41.      */
  42.     public function __construct(
  43.         SystemConfigService $systemConfigService,
  44.         PackstationService $packstationService,
  45.         EntityRepositoryInterface $countryRepository
  46.     )
  47.     {
  48.         $this->systemConfigService $systemConfigService;
  49.         $this->packstationService $packstationService;
  50.         $this->countryRepository $countryRepository;
  51.     }
  52.     public static function getSubscribedEvents(): array
  53.     {
  54.         return [
  55.             StorefrontRenderEvent::class => 'onStorefrontRender',
  56.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'onCustomerAddressWritten',
  57.             AddressListingPageLoadedEvent::class => 'onAddressListingPageLoaded',
  58.             AddressDetailPageLoadedEvent::class => 'onAddressDetailPageLoaded',
  59.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoaded',
  60.             AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoaded'
  61.         ];
  62.     }
  63.     public function onStorefrontRender(StorefrontRenderEvent $event)
  64.     {
  65.         if (!$this->isPluginActive($event->getSalesChannelContext()->getSalesChannel()->getId())) {
  66.             return;
  67.         }
  68.         $config $this->systemConfigService->get(TonurPackstation6::BUNDLE_NAME '.config');
  69.         $event->setParameter('repertusPackstationConfig'$config);
  70.     }
  71.     /**
  72.      * @param EntityWrittenEvent $event
  73.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  74.      */
  75.     public function onCustomerAddressWritten(EntityWrittenEvent $event): void
  76.     {
  77.         if (!$this->isPluginActive() ||
  78.             $event->getEntityName() !== CustomerAddressDefinition::ENTITY_NAME) {
  79.             return;
  80.         }
  81.         foreach ($event->getWriteResults() as $writeResult) {
  82.             $customerAddress $writeResult->getPayload();
  83.             $packstationInfo $this->packstationService->getPackstationInfo($customerAddress$event->getContext());
  84.             $this->logger->debug(__METHOD__, ['packstationInfo' => $packstationInfo'customerAddress' => $customerAddress]);
  85.             if ($packstationInfo !== false) {
  86.                 $this->packstationService->savePackstation(
  87.                     $packstationInfo,
  88.                     $event->getContext()
  89.                 );
  90.             } else if ($this->addressContainsMainData($customerAddress)) {
  91.                 $this->packstationService->deletePackstationByAddressId($customerAddress['id'], $event->getContext());
  92.             }
  93.         }
  94.     }
  95.     public function onAddressListingPageLoaded(AddressListingPageLoadedEvent $event)
  96.     {
  97.         if (!$this->isPluginActive($event->getSalesChannelContext()->getSalesChannel()->getId())) {
  98.             return;
  99.         }
  100.         $request $event->getRequest();
  101.         $changeableAddresses $request->get('changeableAddresses');
  102.         $isChangeBilling \is_array($changeableAddresses) && array_key_exists('changeBilling'$changeableAddresses) ? $changeableAddresses['changeBilling'] : null;
  103.         $page $event->getPage();
  104.         $extensions $page->getExtensions();
  105.         $extensions['tonurPackstationCountryUuidGermany'] = $this->getCountryUuidByIso($event->getContext());
  106.         if ($isChangeBilling) {
  107.             $filteredAddresses = new CustomerAddressCollection();
  108.             $userAddresses $page->getAddresses();
  109.             foreach ($userAddresses as $address) {
  110.                 $packstationExtension $address->getExtension('repertusPackstation');
  111.                 if ($packstationExtension instanceof PackstationEntity) {
  112.                     continue;
  113.                 }
  114.                 $filteredAddresses->add($address);
  115.             }
  116.             $page->setAddresses($filteredAddresses);
  117.             $extensions['tonurPackstationHasPackstationAddresses'] = $filteredAddresses->count() < $userAddresses->count();
  118.         }
  119.         $page->setExtensions($extensions);
  120.     }
  121.     public function onAddressDetailPageLoaded(AddressDetailPageLoadedEvent $event)
  122.     {
  123.         if (!$this->isPluginActive($event->getSalesChannelContext()->getSalesChannel()->getId())) {
  124.             return;
  125.         }
  126.         $page $event->getPage();
  127.         $extensions $page->getExtensions();
  128.         $extensions['tonurPackstationCountryUuidGermany'] = $this->getCountryUuidByIso($event->getContext());
  129.         $page->setExtensions($extensions);
  130.     }
  131.     public function onCheckoutRegisterPageLoaded(CheckoutRegisterPageLoadedEvent $event)
  132.     {
  133.         if (!$this->isPluginActive($event->getSalesChannelContext()->getSalesChannel()->getId())) {
  134.             return;
  135.         }
  136.         $page $event->getPage();
  137.         $extensions $page->getExtensions();
  138.         $extensions['tonurPackstationCountryUuidGermany'] = $this->getCountryUuidByIso($event->getContext());
  139.         $page->setExtensions($extensions);
  140.     }
  141.     public function onAccountLoginPageLoaded(AccountLoginPageLoadedEvent $event)
  142.     {
  143.         if (!$this->isPluginActive($event->getSalesChannelContext()->getSalesChannel()->getId())) {
  144.             return;
  145.         }
  146.         $page $event->getPage();
  147.         $extensions $page->getExtensions();
  148.         $extensions['tonurPackstationCountryUuidGermany'] = $this->getCountryUuidByIso($event->getContext());
  149.         $page->setExtensions($extensions);
  150.     }
  151.     private function getCountryUuidByIso($context$iso 'DE')
  152.     {
  153.         $criteria = (new Criteria())
  154.             ->addFilter(new EqualsFilter('country.active'true))
  155.             ->addFilter(new EqualsFilter('country.iso'$iso));
  156.         /** @var CountryCollection $countries */
  157.         $countries $this->countryRepository->search($criteria$context)->getEntities();
  158.         $country $countries->first();
  159.         return $country $country->getId() : null;
  160.     }
  161.     private function isPluginActive(?string $salesChannelId null)
  162.     {
  163.         return $this->systemConfigService->get(TonurPackstation6::BUNDLE_NAME '.config.pluginActive'$salesChannelId);
  164.     }
  165.     private function addressContainsMainData($customerAddress)
  166.     {
  167.         return array_key_exists('customerId'$customerAddress) &&
  168.             array_key_exists('street'$customerAddress) &&
  169.             array_key_exists('zipcode'$customerAddress) &&
  170.             array_key_exists('city'$customerAddress) &&
  171.             array_key_exists('countryId'$customerAddress) &&
  172.             array_key_exists('firstName'$customerAddress) &&
  173.             array_key_exists('lastName'$customerAddress);
  174.     }
  175. }