src/EventSubscriber/SupplySubscriber.php line 54

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ArticleSupply;
  4. use App\Entity\Supply;
  5. use App\Entity\User;
  6. use App\Event\Supply\SupplyApprovedEvent;
  7. use App\Event\Supply\SupplyCreatedEvent;
  8. use App\Event\Supply\SupplyDeletedEvent;
  9. use App\Event\Supply\SupplyUpdatedEvent;
  10. use App\Repository\SupplyRepository;
  11. use App\Service\NotificationService;
  12. use DateTime;
  13. use Symfony\Bundle\SecurityBundle\Security;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class SupplySubscriber implements EventSubscriberInterface
  16. {
  17.     public function __construct(
  18.         private Security $security,
  19.         private NotificationService $notificationService,
  20.         private SupplyRepository $supplyRepository
  21.     ){}
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             SupplyCreatedEvent::class => 'onCreated',
  26.             SupplyUpdatedEvent::class => 'onUpdated',
  27.             SupplyApprovedEvent::class => 'onApproved',
  28.             SupplyDeletedEvent::class => 'onDeleted',
  29.         ];
  30.     }
  31.     public function onCreated(SupplyCreatedEvent $event): void
  32.     {
  33.         $supply $event->getSupply();
  34.         if ($supply instanceof Supply) {
  35.             $this->notificationService->notifyUser(
  36.                 $supply->getAuthor(),
  37.                 'Approvisionnement ajouté',
  38.                 sprintf(
  39.                     "Vous venez d'ajouter l'approvisionnement <strong>%s</strong>",
  40.                     $supply->getCode()
  41.                 ),
  42.                 $supply
  43.             );
  44.         }
  45.     }
  46.     public function onUpdated(SupplyUpdatedEvent $event): void
  47.     {
  48.         $supply $event->getSupply();
  49.         if ($supply instanceof Supply) {
  50.             $this->notificationService->notifyUser(
  51.                 $supply->getAuthor(),
  52.                 'Approvisionnement modifié',
  53.                 sprintf(
  54.                     "Vous venez de modifier l'approvisionnement <strong>%s</strong>",
  55.                     $supply->getCode()
  56.                 ),
  57.                 $supply
  58.             );
  59.         }
  60.     }
  61.     public function onApproved(SupplyApprovedEvent $event): void
  62.     {
  63.         $supply $event->getSupply();
  64.         /** @var User $user */
  65.         $user $this->security->getUser();
  66.         if ($supply instanceof Supply) {
  67.             $supply->setApprovedBy($user);
  68.             $supply->setIsApproved(1);
  69.             $supply->setApprovedAt(new DateTime());
  70.             /** @var ArticleSupply $article */
  71.             foreach ($supply->getArticles() as $article) {
  72.                 $quantity $article->getArticle()->getQuantity();
  73.                 $article->getArticle()->setQuantity($quantity $article->getQuantity());
  74.                 $article->getArticle()->setLastSupplyAt(new DateTime());
  75.             }
  76.             $this->supplyRepository->save($supply);
  77.             $this->notificationService->notifyUser(
  78.                 $supply->getAuthor(),
  79.                 'Approvisionnement approuvé',
  80.                 sprintf(
  81.                     "Vous venez d'approuver l'approvisionnement <strong>%s</strong>",
  82.                     $supply->getCode()
  83.                 ),
  84.                 $supply
  85.             );
  86.         }
  87.     }
  88.     public function onDeleted(SupplyDeletedEvent $event): void
  89.     {
  90.         $supply $event->getSupply();
  91.         if ($supply instanceof Supply) {
  92.             $this->notificationService->notifyUser(
  93.                 $supply->getAuthor(),
  94.                 'Approvisionnement supprimé',
  95.                 sprintf(
  96.                     "Vous venez de supprimer l'approvisionnement <strong>%s</strong>",
  97.                     $supply->getCode()
  98.                 ),
  99.                 $supply
  100.             );
  101.         }
  102.     }
  103. }