lib/EventToNotificationBundle/src/CompilerPass/EventToNotificationEventSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace EToNBundle\CompilerPass;
  3. use EToNBundle\Builder\EventToNotificationEmail;
  4. use EToNBundle\Entity\EventToNotificationStore;
  5. use EToNBundle\Repository\EventToNotificationStoreRepository;
  6. use EToNBundle\Resolver\EventToNotificationResolver;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Contracts\EventDispatcher\Event;
  11. use Twig\Environment;
  12. class EventToNotificationEventSubscriber
  13. {
  14.     public function __construct(private EventToNotificationStoreRepository $notificationStoreRepository,
  15.                                 private LoggerInterface $logger,
  16.                                 private EventToNotificationResolver $resolver,
  17.                                 private MailerInterface $mailer,
  18.                                 private Environment $twig,
  19.                                 private string $mailFrom,
  20.                                 private ?string $templatenull)
  21.     {}
  22.     public function onEvent(Event $event): void
  23.     {
  24.         $info$this->resolver->get($event);
  25.         if($info){
  26.             $data $this->notificationStoreRepository->findBy(["event" => $info->name"isEnabled" => true]);
  27.             if($data){
  28.                 /** @var EventToNotificationStore $item */
  29.                 foreach ($data as $item) {
  30.                     if($item->isEnabled()){
  31.                         try {
  32.                             $email= new EventToNotificationEmail($info$item$event);
  33.                             $email->build($this->mailFrom);
  34.                             if($this->template){
  35.                                 $view$this->twig->render($this->template, [
  36.                                     "notifEmail" => $email,
  37.                                     "store" => $item,
  38.                                     "event" => $event,
  39.                                 ]);
  40.                                 $email->html($view);
  41.                             }
  42.                             $this->mailer->send($email);
  43.                         } catch (TransportExceptionInterface $e) {
  44.                             $this->logger->error($e->getMessage(), $e->getTrace());
  45.                         }
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }