<?php
declare(strict_types=1);
namespace App\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class DownloadTokenSubscriber
*
* @package App\Subscriber
*/
class DownloadTokenSubscriber implements EventSubscriberInterface
{
/**
*
*/
public const SESSION_KEY_DOWNLOAD_TOKEN = 'downloadToken';
private SessionInterface $session;
/**
* DownloadTokenSubscriber constructor.
*
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest'
];
}
/**
* @param RequestEvent $event
* @return void
*/
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$downloadToken = $request->query->get('downloadToken');
if ($downloadToken) {
$this->session->set(self::SESSION_KEY_DOWNLOAD_TOKEN, $downloadToken);
}
}
}