src/EventSubscriber/Api/EstimateOwnerSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. // api/src/EventSubscriber/BookMailSubscriber.php
  3. namespace App\EventSubscriber\Api;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\Legacy\Estimate;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. final class EstimateOwnerSubscriber implements EventSubscriberInterface
  11. {
  12. /**
  13. * @var TokenStorageInterface
  14. */
  15. private $tokenStorage;
  16. public function __construct(TokenStorageInterface $tokenStorage)
  17. {
  18. $this->tokenStorage = $tokenStorage;
  19. }
  20. public static function getSubscribedEvents()
  21. {
  22. return [
  23. KernelEvents::VIEW => ['validate', EventPriorities::PRE_VALIDATE],
  24. ];
  25. }
  26. public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
  27. {
  28. if (!$event->getControllerResult() instanceof Estimate) {
  29. return;
  30. }
  31. if ($event->getControllerResult()->getOwner() !== null) {
  32. return;
  33. }
  34. $event->getControllerResult()->setOwner($this->tokenStorage->getToken()->getUser());
  35. }
  36. }