<?php declare(strict_types=1);
namespace AgiqonOci\Subscriber;
use AgiqonOci\Models\CxmlSession;
use AgiqonOci\Models\OciSession;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
class InspectSubscriber implements EventSubscriberInterface
{
public const INSPECT_URI = '/AgiqonCxmlEntry/inspect';
public const LOGOUT_URI = '/AgiqonOciEntry/logout';
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => 'onKernelResponse'
];
}
public function onKernelResponse(ResponseEvent $event)
{
$requestUri = $event->getRequest()->getRequestUri();
try {
$ociSession = $event->getRequest()->getSession()->get(OciSession::OCI_SESSION_NAME);
} catch (SessionNotFoundException $exception) {
return;
}
if ($ociSession instanceof CxmlSession
&& $ociSession->getSystem() === 'cxml'
&& $ociSession->getOperation() === 'inspect'
&& $requestUri !== self::INSPECT_URI
&& $requestUri !== self::LOGOUT_URI
&& !$this->isWdt($requestUri)) {
$redirectResponse = new RedirectResponse(self::INSPECT_URI);
$event->setResponse($redirectResponse);
}
}
private function isWdt(string $requestUri): bool
{
return strpos($requestUri, '_wdt') !== false;
}
}