<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
// use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Entity\Policy;
// use App\Entity\User;
final class PolicySubscriber implements EventSubscriberInterface
{
// private $tokenStorage;
public function __construct(
// TokenStorageInterface $tokenStorage,
) {
// $this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['updateDemandStatus', EventPriorities::PRE_WRITE],
],
];
}
public function updateDemandStatus(ViewEvent $event)
{
$policy = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$policy instanceof Policy || (Request::METHOD_PUT !== $method && Request::METHOD_POST !== $method)) {
return;
}
// $token = $this->tokenStorage->getToken();
// if (!$token) {
// return;
// }
// $user = $token->getUser();
// if (!$user instanceof User) {
// return;
// }
if ($policy->getStatus() === "Issued") {
$policy->getDemand()->setStatus("Issued");
}
}
}