vendor/store.shopware.com/nrlejpostdirektautocomplete/src/Subscriber/AddressSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3.  * See LICENSE.md for license details.
  4.  */
  5. declare (strict_types=1);
  6. namespace PostDirekt\Autocomplete\Subscriber;
  7. use PostDirekt\Autocomplete\Service\AuthenticationService;
  8. use PostDirekt\Autocomplete\Service\ModuleConfig;
  9. use Shopware\Core\Framework\Struct\ArrayEntity;
  10. use Shopware\Storefront\Page\Account\Login\AccountLoginPage;
  11. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  12. use Shopware\Storefront\Page\Address\Detail\AddressDetailPage;
  13. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  14. use Shopware\Storefront\Page\Address\Listing\AddressListingPage;
  15. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPage;
  17. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  18. use Shopware\Storefront\Page\PageLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. /**
  21.  * Subscribes AccountLoginPageLoadedEvent,AddressListingPageLoadedEvent, CheckoutRegisterPageLoadedEvent,
  22.  * AddressDetailPageLoadedEvent and provides api token to page objects if autocomplete service is enabled.
  23.  *
  24.  * @author   Andreas Müller <andreas.mueller@netresearch.de>
  25.  *
  26.  * @see     https://www.netresearch.de/
  27.  */
  28. class AddressSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
  29. {
  30.     public const TOKEN_KEY 'token';
  31.     public const PAGE_EXTENSION_KEY 'postdirekt_autocomplete';
  32.     public const HINT 'hint';
  33.     /**
  34.      * @var AuthenticationService
  35.      */
  36.     private $authService;
  37.     /**
  38.      * @var ModuleConfig
  39.      */
  40.     private $moduleConfig;
  41.     public function __construct(\PostDirekt\Autocomplete\Service\AuthenticationService $authService\PostDirekt\Autocomplete\Service\ModuleConfig $moduleConfig)
  42.     {
  43.         $this->authService $authService;
  44.         $this->moduleConfig $moduleConfig;
  45.     }
  46.     public static function getSubscribedEvents() : array
  47.     {
  48.         return [\Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent::class => 'onAddressPagesLoaded'\Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent::class => 'onAddressPagesLoaded'\Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent::class => 'onAddressPagesLoaded'\Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent::class => 'onAddressPagesLoaded'];
  49.     }
  50.     /**
  51.      * When page is loaded, fetch API token and pass it into the page context for templates to use
  52.      *
  53.      * @param AccountLoginPageLoadedEvent|AddressListingPageLoadedEvent|CheckoutRegisterPageLoadedEvent|AddressDetailPageLoadedEvent|PageLoadedEvent $event
  54.      */
  55.     public function onAddressPagesLoaded(\Shopware\Storefront\Page\PageLoadedEvent $event) : void
  56.     {
  57.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  58.         if (!$this->moduleConfig->isActive($salesChannelId)) {
  59.             // Deactivated by configuration
  60.             return;
  61.         }
  62.         try {
  63.             $token $this->authService->fetchToken($salesChannelId);
  64.         } catch (\RuntimeException $exception) {
  65.             /*
  66.              * Logging is already done in SDK, we will just early return here to avoid rendering of
  67.              * our element in the templates
  68.              */
  69.             return;
  70.         }
  71.         $hint $this->moduleConfig->isHouseNumberHintActive($salesChannelId) ?: null;
  72.         /** @var AccountLoginPage|AddressListingPage|CheckoutRegisterPage|AddressDetailPage $page */
  73.         $page $event->getPage();
  74.         $page->addExtension(self::PAGE_EXTENSION_KEY, new \Shopware\Core\Framework\Struct\ArrayEntity([self::TOKEN_KEY => $tokenself::HINT => $hint]));
  75.     }
  76. }