<?php
declare(strict_types=1);
namespace App\Bundles\OrganizationBundle\EventListener;
use App\Bundles\OrganizationBundle\Service\UserOrganizationApiKey\UserOrganizationApiKeyService;
use App\Platform\Service\SessionProvider;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
#[AsEventListener(event: ResponseEvent::class, method: 'onKernelRequest')]
class CheckApiKeyExistEventListener
{
public const AUTH_HEADER = 'Authentication';
public function __construct(
private readonly SessionProvider $sessionProvider,
) {
}
public function onKernelRequest(ResponseEvent $event): void
{
$response = $event->getResponse();
$session = $this->sessionProvider->provide();
if (!$session->has(UserOrganizationApiKeyService::SESSION_API_KEY)) {
return;
}
$cookie = new Cookie(
name: self::AUTH_HEADER,
value: $session->get(UserOrganizationApiKeyService::SESSION_API_KEY),
httpOnly: false
);
$response->headers->setCookie($cookie);
}
}