<?php declare(strict_types=1);
namespace GlobusSW6\Subscriber\Session;
use GlobusSW6\Service\App\AppService;
use GlobusSW6\Service\StoreLocator\StoreSwitchService;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class SessionSubscriber implements EventSubscriberInterface
{
/** @var StoreSwitchService */
private $storeSwitchService;
/** @var AppService */
private $appService;
/**
* @param StoreSwitchService $storeSwitchService
* @param AppService $appService
*/
public function __construct(StoreSwitchService $storeSwitchService, AppService $appService)
{
$this->storeSwitchService = $storeSwitchService;
$this->appService = $appService;
}
public static function getSubscribedEvents()
{
return [
GenericPageLoadedEvent::class => 'appendSessionVariables'
];
}
public function appendSessionVariables(GenericPageLoadedEvent $event)
{
/** @var Session $session */
$session = $event->getRequest()->getSession();
$salesChannelContext = $event->getSalesChannelContext();
$selectedStore = $session->get('ianeoCurrentStore');
if (is_null($selectedStore)) { // there always has to be a selected store: set fallback store
$session->set("setFallbackStoreInProgress","start");
$this->storeSwitchService->setFallbackStore($session, $salesChannelContext);
$session->set("setFallbackStoreInProgress","end");
}
$event->getPage()->assign([
'ianeoHitsellerIsApp' => $this->appService->isAppFromSession($session),
'ianeoCurrentStore' => $session->get('ianeoCurrentStore'),
'ianeoIsFallbackStore' => $session->get('isFallbackStore'),
'ianeoCartViolations' => $session->get('ianeoCartViolations'),
'offcanvasModal' => $session->get('offcanvasModal')
]);
}
}