<?php
declare(strict_types=1);
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class CouponCodeListener
{
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
public function onKernelRequest(RequestEvent $event)
{
// Check if the request is a master request (HTTP request)
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
// Exclude requests with path starting with /admin
if (strpos($request->getPathInfo(), '/admin') === 0) {
return;
}
$couponCode = $request->query->get('ref');
if ($couponCode) {
$this->session->set('couponCode', $couponCode);
}
}
}