vendor/roothirsch/shop-bundle/EventSubscriber/Api/EstimateDistributorSubscriber.php line 39

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\CoreBundle\Entity\Company;
  6. use Roothirsch\CoreBundle\Entity\User;
  7. use Roothirsch\ShopBundle\Entity\Estimate;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. final class EstimateDistributorSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var TokenStorageInterface
  15.      */
  16.     private $tokenStorage;
  17.     /**
  18.      * @var User
  19.      */
  20.     private $user;
  21.     public function __construct(TokenStorageInterface $tokenStorage)
  22.     {
  23.         $this->tokenStorage $tokenStorage;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::VIEW => ['validate'EventPriorities::PRE_VALIDATE],
  29.         ];
  30.     }
  31.     public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
  32.     {
  33.         /** @var Estimate $estimate */
  34.         $estimate $event->getControllerResult();
  35.         if (!$estimate instanceof Estimate) {
  36.             return;
  37.         }
  38.         if ($estimate->getDistributor() !== null) {
  39.             return;
  40.         }
  41.         $this->user $this->tokenStorage->getToken()->getUser();
  42.         if (!$this->user->getCompany() instanceof Company) {
  43.             return;
  44.         }
  45.         $event->getControllerResult()->setDistributor($this->user->getCompany()->getDistributor());
  46.     }
  47. }