<?php declare(strict_types=1);
namespace GlobusSW6\Subscriber\App;
use GlobusSW6\Service\App\AppFromRequestIdentifier;
use GlobusSW6\Service\App\AppService;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Util\Random;
use Shopware\Core\PlatformRequest;
use Shopware\Core\SalesChannelRequest;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class SessionSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
/** @var AppFromRequestIdentifier */
private $appIdentifier;
/** @var LoggerInterface */
private $logger;
/** @var AppService */
private $appService;
/**
* @param RequestStack $requestStack
* @param AppFromRequestIdentifier $appIdentifier
* @param LoggerInterface $logger
* @param AppService $appService
*/
public function __construct(RequestStack $requestStack, AppFromRequestIdentifier $appIdentifier, LoggerInterface $logger, AppService $appService)
{
$this->requestStack = $requestStack;
$this->appIdentifier = $appIdentifier;
$this->logger = $logger;
$this->appService = $appService;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [ // Overwrite session functionality from Storefront Subscriber
['startSession', 39]
]
];
}
public function startSession()
{
try{
$master = $this->requestStack->getMainRequest();
if (!$master) {
return;
}
if (!$master->hasSession()) {
return;
}
/** @var SessionInterface $session */
$session = $master->getSession();
/** @var HeaderBag $header */
$header = $master->headers;
// If FBO-route /show-header : set ianeoCurrentStore for this route -> This will enforce the twig header to contain the desired id
try {
if (strpos($master->getPathInfo(), '/show-header') !== false){
if (!is_null($master->get('storeId'))) {
$session->set('ianeoCurrentStore', $master->get('storeId'));
return;
}
}
} catch (\Throwable $t){
$this->logger->info('unable to set ianeoCurrentStore for /show-header: ' . $t->getMessage());
}
if (!$this->appIdentifier->isApp($master)){
return;
}
$device_id = $header->get('x-globus-device-id');
if (is_null($master->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN))){
$this->appService->saveDevice($device_id, Random::getAlphanumericString(32));
} else {
$this->appService->saveDevice($device_id, $master->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN));
}
$device = $this->appService->getAppDeviceById($device_id);
if (!$master->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST) && is_null($device)) {
return;
}
// Check if request is from app and if session-token has right value for app - if not, set sw-context-token and selected store for app
if ($this->appIdentifier->isApp($master) && $session->get(PlatformRequest::HEADER_CONTEXT_TOKEN) !== $device->getSwContextToken()) {
$session->set(PlatformRequest::HEADER_CONTEXT_TOKEN, $device->getSwContextToken());
$session->set('x-globus-customer', $device->getCustomerId());
$session->set('x-globus-device-id', $device->getDeviceId());
if (!is_null($device->getStoreLegacyId())){
$session->set('ianeoCurrentStore', $device->getStoreLegacyId());
}
$master->headers->set(
PlatformRequest::HEADER_CONTEXT_TOKEN,
$session->get(PlatformRequest::HEADER_CONTEXT_TOKEN)
);
}
} catch (\Throwable $t) {
$this->logger->error(__CLASS__ . ":" . __FUNCTION__ . ":" . __LINE__ . ":\$exception->getMessage(): " . $t->getMessage());
}
}
}