src/EventSubscriber/Vendor/VendorPostWriteSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: sebastiantovar
  5.  * Date: 2019-04-03
  6.  * Time: 16:47
  7.  */
  8. namespace App\EventSubscriber\Vendor;
  9. use ApiPlatform\Core\EventListener\EventPriorities;
  10. use App\Entity\App\User;
  11. use App\Entity\App\Vendor;
  12. use App\MessageHandler\Message;
  13. use App\Services\VendorService;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Event\ViewEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Messenger\MessageBusInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. class VendorPostWriteSubscriber implements EventSubscriberInterface
  22. {
  23.     private $vendorService;
  24.     private $messageBus;
  25.     private $parameterBag;
  26.     private $tokenStorage;
  27.     /**
  28.      * VendorPostWriteSubscriber constructor.
  29.      * @param TokenStorageInterface $tokenStorage
  30.      * @param ParameterBagInterface $parameterBag
  31.      * @param VendorService $vendorService
  32.      * @param MessageBusInterface $messageBus
  33.      */
  34.     public function __construct(   TokenStorageInterface $tokenStorage,ParameterBagInterface $parameterBagVendorService $vendorServiceMessageBusInterface $messageBus ) {
  35.         $this->vendorService $vendorService;
  36.         $this->messageBus $messageBus;
  37.         $this->parameterBag $parameterBag;
  38.         $this->tokenStorage $tokenStorage;
  39.     }
  40.     public function onKernelView(ViewEvent $event)
  41.     {
  42.         $vendor $event->getControllerResult();
  43.         $request $event->getRequest();
  44.         $method $request->getMethod();
  45.         $route $request->attributes->get('_route');
  46.         if (!($vendor instanceof Vendor))
  47.             return;
  48.         if (Request::METHOD_POST === $method) {
  49.             $data=[];
  50.             $userCurrent $this->tokenStorage->getToken()->getUser();
  51.             $data['from'] = $this->parameterBag->has('ONBOARDING_FROM') ? $this->parameterBag->get('ONBOARDING_FROM') : null;
  52.             if(!is_null($data['from']) && $userCurrent instanceof User && in_array(trim($userCurrent->getEmail()),explode(",",$data['from']))) {
  53.                 $data["vendor"] = $vendor;
  54.                 $data["to"] = $this->parameterBag->has('ONBOARDING_TO') ? $this->parameterBag->get('ONBOARDING_TO') : null;
  55.                 $this->messageBus->dispatch(new Message(Message::NEW_VENDOR$data));
  56.             }
  57.         }
  58.         if ('api_vendors_post_collection' == $route || 'api_vendors_put_item' == $route) {
  59.             $mediaContent $vendor->getMediaContent();
  60.             if ($mediaContent !== null && $mediaContent !== 0)
  61.                 $this->vendorService->addMediaContent($vendor$mediaContent);
  62.         }
  63.     }
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_WRITE]
  68.         ];
  69.     }
  70. }