src/EventSubscriber/ArticleSubscriber.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Api\IriConverterInterface;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Constant\ApiConstants;
  6. use App\Entity\Article;
  7. use App\Entity\Colis;
  8. use App\Entity\Supply;
  9. use App\Service\FileUploader;
  10. use App\Service\SmsService;
  11. use App\Service\Utils;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Security;
  19. use Vich\UploaderBundle\Storage\StorageInterface;
  20. class ArticleSubscriber implements EventSubscriberInterface
  21. {
  22.     private $managerRegistry;
  23.     private $utils;
  24.     private $iriConverter;
  25.     private $fileUploader;
  26.     private $tokenStorage;
  27.     private $smsService;
  28.     private $userConnected;
  29.     public function __construct(ManagerRegistry $managerRegistry,Utils $utils,
  30.                                 IriConverterInterface $converter,
  31.                                 FileUploader $fileUploader,
  32.                                 Security $security,
  33.                                 TokenStorageInterface $tokenStorage,
  34.                                 SmsService $smsService)
  35.     {
  36.         $this->managerRegistry $managerRegistry;
  37.         $this->utils $utils;
  38.         $this->iriConverter $converter;
  39.         $this->fileUploader $fileUploader;
  40.         $this->tokenStorage $tokenStorage;
  41.         $this->smsService $smsService;
  42.         $this->userConnected $security->getUser();
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             KernelEvents::VIEW => [
  48.                 //['processColis', EventPriorities::PRE_WRITE],
  49.                 ['postArticle'EventPriorities::POST_WRITE],
  50.                 //['processReception',EventPriorities::PRE_WRITE],
  51.             ],
  52.         ];
  53.     }
  54.     public function postArticle(ViewEvent $event){
  55.         $article $event->getControllerResult();
  56.         $method $event->getRequest()->getMethod();
  57.         if (!($article instanceof Article && Request::METHOD_POST === $method && $event->getRequest()->getPathInfo() === '/articles')) {
  58.             return ;
  59.         }
  60.         $supply = new Supply();
  61.         $supply->setOperation('approvisionnement');
  62.         $supply->setQuantity($article->getQuantity());
  63.         $supply->setArticle($article);
  64.         $supply->setUser($this->userConnected);
  65.         $supply->setInitialQuantity(0);
  66.         $this->managerRegistry->getManager()->persist($supply);
  67.         $this->managerRegistry->getManager()->flush();
  68.     }
  69. }