vendor/roothirsch/shop-bundle/EventSubscriber/Api/EstimateOwnerSubscriber.php line 32

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