<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 14 maj 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\ShoppingList\Subscriber;
use Crehler\ShoppingList\CrehlerShoppingList;
use Crehler\ShoppingList\Service\ShoppingListService;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class StorefrontSubscriber implements EventSubscriberInterface
{
private const SAVE_LIST_KEY = 'saveListIdKey';
private ShoppingListService $shoppingListService;
private Session $session;
private $translator;
public function __construct(ShoppingListService $shoppingListService, Session $session, $translator)
{
$this->shoppingListService = $shoppingListService;
$this->session = $session;
$this->translator = $translator;
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender',
CustomerLoginEvent::class => 'onCustomerLogin',
CustomerRegisterEvent::class => 'onCustomerRegister',
AccountLoginPageLoadedEvent::class => 'onAccountLoginPage',
];
}
public function onCustomerLogin(CustomerLoginEvent $event)
{
$customer = $event->getCustomer();
if ($this->session->has(self::SAVE_LIST_KEY)) {
$this->shoppingListService->saveListToCustomer(
$this->session->get(self::SAVE_LIST_KEY),
$customer->getId(),
$event->getContext()
);
$this->session->getFlashBag()->add('success', $this->trans('shoppingList.shoppingListSaveToAccountSuccess'));
$this->session->remove(self::SAVE_LIST_KEY);
}
}
public function onCustomerRegister(CustomerRegisterEvent $event)
{
$customer = $event->getCustomer();
if ($this->session->has(self::SAVE_LIST_KEY)) {
$this->shoppingListService->saveListToCustomer(
$this->session->get(self::SAVE_LIST_KEY),
$customer->getId(),
$event->getContext()
);
$this->session->getFlashBag()->add('success', $this->trans('shoppingList.shoppingListSaveToAccountSuccess'));
$this->session->remove(self::SAVE_LIST_KEY);
}
}
public function onAccountLoginPage(AccountLoginPageLoadedEvent $event)
{
$request = $event->getRequest();
$saveListId = $request->get('saveListId');
if ($saveListId !== null) {
$this->session->set(self::SAVE_LIST_KEY, $saveListId);
}
}
public function onStorefrontRender(StorefrontRenderEvent $event): void
{
$salesChannelContext = $event->getSalesChannelContext();
$token = $event->getRequest()->cookies->get(CrehlerShoppingList::SHOPPING_LIST_COOKIE_TOKEN_NAME);
$isCustomer = $salesChannelContext->getCustomer() instanceof CustomerEntity;
$shoppingLists = $this->shoppingListService->getShoppingLists(
$isCustomer ? $salesChannelContext->getCustomer()->getId() : null,
$token,
$event->getSalesChannelContext()
);
$event->getSalesChannelContext()->addExtension('shoppingLists', $shoppingLists);
}
protected function trans(string $snippet, array $parameters = []): string
{
return $this->translator->trans($snippet, $parameters);
}
}