custom/plugins/NRLEJPostDirektAutocomplete/src/Subscriber/AddressSubscriber.php line 72

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 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(
  42.         AuthenticationService $authService,
  43.         ModuleConfig $moduleConfig
  44.     ) {
  45.         $this->authService $authService;
  46.         $this->moduleConfig $moduleConfig;
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             AccountLoginPageLoadedEvent::class => 'onAddressPagesLoaded',
  52.             AddressListingPageLoadedEvent::class => 'onAddressPagesLoaded',
  53.             CheckoutRegisterPageLoadedEvent::class => 'onAddressPagesLoaded',
  54.             AddressDetailPageLoadedEvent::class => 'onAddressPagesLoaded',
  55.         ];
  56.     }
  57.     /**
  58.      * When page is loaded, fetch API token and pass it into the page context for templates to use
  59.      *
  60.      * @param AccountLoginPageLoadedEvent|AddressListingPageLoadedEvent|CheckoutRegisterPageLoadedEvent|AddressDetailPageLoadedEvent|PageLoadedEvent $event
  61.      */
  62.     public function onAddressPagesLoaded(PageLoadedEvent $event): void
  63.     {
  64.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  65.         if (!$this->moduleConfig->isActive($salesChannelId)) {
  66.             // Deactivated by configuration
  67.             return;
  68.         }
  69.         try {
  70.             $token $this->authService->fetchToken($salesChannelId);
  71.         } catch (\RuntimeException $exception) {
  72.             /*
  73.              * Logging is already done in SDK, we will just early return here to avoid rendering of
  74.              * our element in the templates
  75.              */
  76.             return;
  77.         }
  78.         $hint $this->moduleConfig->isHouseNumberHintActive($salesChannelId) ?: null;
  79.         /** @var AccountLoginPage|AddressListingPage|CheckoutRegisterPage|AddressDetailPage $page */
  80.         $page $event->getPage();
  81.         $page->addExtension(
  82.             self::PAGE_EXTENSION_KEY,
  83.             new ArrayEntity(
  84.                 [self::TOKEN_KEY => $tokenself::HINT => $hint]
  85.             )
  86.         );
  87.     }
  88. }