<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Constant\ApiConstants;
use App\Entity\Article;
use App\Entity\Colis;
use App\Entity\Supply;
use App\Service\FileUploader;
use App\Service\SmsService;
use App\Service\Utils;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Security;
use Vich\UploaderBundle\Storage\StorageInterface;
class ArticleSubscriber implements EventSubscriberInterface
{
private $managerRegistry;
private $utils;
private $iriConverter;
private $fileUploader;
private $tokenStorage;
private $smsService;
private $userConnected;
public function __construct(ManagerRegistry $managerRegistry,Utils $utils,
IriConverterInterface $converter,
FileUploader $fileUploader,
Security $security,
TokenStorageInterface $tokenStorage,
SmsService $smsService)
{
$this->managerRegistry = $managerRegistry;
$this->utils = $utils;
$this->iriConverter = $converter;
$this->fileUploader = $fileUploader;
$this->tokenStorage = $tokenStorage;
$this->smsService = $smsService;
$this->userConnected = $security->getUser();
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
//['processColis', EventPriorities::PRE_WRITE],
['postArticle', EventPriorities::POST_WRITE],
//['processReception',EventPriorities::PRE_WRITE],
],
];
}
public function postArticle(ViewEvent $event){
$article = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!($article instanceof Article && Request::METHOD_POST === $method && $event->getRequest()->getPathInfo() === '/articles')) {
return ;
}
$supply = new Supply();
$supply->setOperation('approvisionnement');
$supply->setQuantity($article->getQuantity());
$supply->setArticle($article);
$supply->setUser($this->userConnected);
$supply->setInitialQuantity(0);
$this->managerRegistry->getManager()->persist($supply);
$this->managerRegistry->getManager()->flush();
}
}