src/EventSubscriber/PolicySubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\Event\ViewEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. // use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use App\Entity\Policy;
  10. // use App\Entity\User;
  11. final class PolicySubscriber implements EventSubscriberInterface
  12. {
  13.     // private $tokenStorage;
  14.     public function __construct(
  15.         // TokenStorageInterface $tokenStorage,
  16.     ) {
  17.         // $this->tokenStorage = $tokenStorage;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             KernelEvents::VIEW => [
  23.                 ['updateDemandStatus'EventPriorities::PRE_WRITE],
  24.             ],
  25.         ];
  26.     }
  27.     public function updateDemandStatus(ViewEvent $event)
  28.     {
  29.         $policy $event->getControllerResult();
  30.         $method $event->getRequest()->getMethod();
  31.         if (!$policy instanceof Policy || (Request::METHOD_PUT !== $method && Request::METHOD_POST !== $method)) {
  32.             return;
  33.         }
  34.         // $token = $this->tokenStorage->getToken();
  35.         // if (!$token) {
  36.         //     return;
  37.         // }
  38.         // $user = $token->getUser();
  39.         // if (!$user instanceof User) {
  40.         //     return;
  41.         // }
  42.         if ($policy->getStatus() === "Issued") {
  43.             $policy->getDemand()->setStatus("Issued");
  44.         }
  45.     }
  46. }