vendor/crehler/shopping-list/Subscriber/StorefrontSubscriber.php line 94

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   14 maj 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\ShoppingList\Subscriber;
  13. use Crehler\ShoppingList\CrehlerShoppingList;
  14. use Crehler\ShoppingList\Service\ShoppingListService;
  15. use Shopware\Core\Checkout\Customer\CustomerEntity;
  16. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  17. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  18. use Shopware\Storefront\Event\StorefrontRenderEvent;
  19. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpFoundation\Session\Session;
  22. class StorefrontSubscriber implements EventSubscriberInterface
  23. {
  24.     private const SAVE_LIST_KEY 'saveListIdKey';
  25.     private ShoppingListService $shoppingListService;
  26.     private Session $session;
  27.     private $translator;
  28.     public function __construct(ShoppingListService $shoppingListServiceSession $session$translator)
  29.     {
  30.         $this->shoppingListService $shoppingListService;
  31.         $this->session $session;
  32.         $this->translator $translator;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             StorefrontRenderEvent::class => 'onStorefrontRender',
  38.             CustomerLoginEvent::class => 'onCustomerLogin',
  39.             CustomerRegisterEvent::class => 'onCustomerRegister',
  40.             AccountLoginPageLoadedEvent::class => 'onAccountLoginPage',
  41.         ];
  42.     }
  43.     public function onCustomerLogin(CustomerLoginEvent $event)
  44.     {
  45.         $customer $event->getCustomer();
  46.         if ($this->session->has(self::SAVE_LIST_KEY)) {
  47.             $this->shoppingListService->saveListToCustomer(
  48.                 $this->session->get(self::SAVE_LIST_KEY),
  49.                 $customer->getId(),
  50.                 $event->getContext()
  51.             );
  52.             $this->session->getFlashBag()->add('success'$this->trans('shoppingList.shoppingListSaveToAccountSuccess'));
  53.             $this->session->remove(self::SAVE_LIST_KEY);
  54.         }
  55.     }
  56.     public function onCustomerRegister(CustomerRegisterEvent $event)
  57.     {
  58.         $customer $event->getCustomer();
  59.         if ($this->session->has(self::SAVE_LIST_KEY)) {
  60.             $this->shoppingListService->saveListToCustomer(
  61.                 $this->session->get(self::SAVE_LIST_KEY),
  62.                 $customer->getId(),
  63.                 $event->getContext()
  64.             );
  65.             $this->session->getFlashBag()->add('success'$this->trans('shoppingList.shoppingListSaveToAccountSuccess'));
  66.             $this->session->remove(self::SAVE_LIST_KEY);
  67.         }
  68.     }
  69.     public function onAccountLoginPage(AccountLoginPageLoadedEvent $event)
  70.     {
  71.         $request $event->getRequest();
  72.         $saveListId $request->get('saveListId');
  73.         if ($saveListId !== null) {
  74.             $this->session->set(self::SAVE_LIST_KEY$saveListId);
  75.         }
  76.     }
  77.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  78.     {
  79.         $salesChannelContext $event->getSalesChannelContext();
  80.         $token $event->getRequest()->cookies->get(CrehlerShoppingList::SHOPPING_LIST_COOKIE_TOKEN_NAME);
  81.         $isCustomer $salesChannelContext->getCustomer() instanceof CustomerEntity;
  82.         $shoppingLists $this->shoppingListService->getShoppingLists(
  83.             $isCustomer $salesChannelContext->getCustomer()->getId() : null,
  84.             $token,
  85.             $event->getSalesChannelContext()
  86.         );
  87.         $event->getSalesChannelContext()->addExtension('shoppingLists'$shoppingLists);
  88.     }
  89.     protected function trans(string $snippet, array $parameters = []): string
  90.     {
  91.         return $this->translator->trans($snippet$parameters);
  92.     }
  93. }