src/EventSubscriber/SupplySubscriber.php line 38
<?phpnamespace App\EventSubscriber;use App\Entity\ArticleSupply;use App\Entity\Supply;use App\Entity\User;use App\Event\Supply\SupplyApprovedEvent;use App\Event\Supply\SupplyCreatedEvent;use App\Event\Supply\SupplyDeletedEvent;use App\Event\Supply\SupplyUpdatedEvent;use App\Repository\SupplyRepository;use App\Service\NotificationService;use DateTime;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class SupplySubscriber implements EventSubscriberInterface{public function __construct(private Security $security,private NotificationService $notificationService,private SupplyRepository $supplyRepository){}public static function getSubscribedEvents(): array{return [SupplyCreatedEvent::class => 'onCreated',SupplyUpdatedEvent::class => 'onUpdated',SupplyApprovedEvent::class => 'onApproved',SupplyDeletedEvent::class => 'onDeleted',];}public function onCreated(SupplyCreatedEvent $event): void{$supply = $event->getSupply();if ($supply instanceof Supply) {$this->notificationService->notifyUser($supply->getAuthor(),'Approvisionnement ajouté',sprintf("Vous venez d'ajouter l'approvisionnement <strong>%s</strong>",$supply->getCode()),$supply);}}public function onUpdated(SupplyUpdatedEvent $event): void{$supply = $event->getSupply();if ($supply instanceof Supply) {$this->notificationService->notifyUser($supply->getAuthor(),'Approvisionnement modifié',sprintf("Vous venez de modifier l'approvisionnement <strong>%s</strong>",$supply->getCode()),$supply);}}public function onApproved(SupplyApprovedEvent $event): void{$supply = $event->getSupply();/** @var User $user */$user = $this->security->getUser();if ($supply instanceof Supply) {$supply->setApprovedBy($user);$supply->setIsApproved(1);$supply->setApprovedAt(new DateTime());/** @var ArticleSupply $article */foreach ($supply->getArticles() as $article) {$quantity = $article->getArticle()->getQuantity();$article->getArticle()->setQuantity($quantity + $article->getQuantity());$article->getArticle()->setLastSupplyAt(new DateTime());}$this->supplyRepository->save($supply);$this->notificationService->notifyUser($supply->getAuthor(),'Approvisionnement approuvé',sprintf("Vous venez d'approuver l'approvisionnement <strong>%s</strong>",$supply->getCode()),$supply);}}public function onDeleted(SupplyDeletedEvent $event): void{$supply = $event->getSupply();if ($supply instanceof Supply) {$this->notificationService->notifyUser($supply->getAuthor(),'Approvisionnement supprimé',sprintf("Vous venez de supprimer l'approvisionnement <strong>%s</strong>",$supply->getCode()),$supply);}}}