src/Notification/EventSubscriber/SendCreatedNotificationSubscriber.php line 48

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2021 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2021 TECLA Consulting Group oü
  16.  */
  17. namespace App\Notification\EventSubscriber;
  18. use App\Notification\Event\Notification\CreatedEvent;
  19. use App\Notification\Exception\EmptyRecipientEmailExceptionInterface;
  20. use App\Notification\Util\NotificationEmailSenderInterface;
  21. use JetBrains\PhpStorm\ArrayShape;
  22. use Psr\Log\LoggerInterface;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. final class SendCreatedNotificationSubscriber implements EventSubscriberInterface
  25. {
  26.     private NotificationEmailSenderInterface $emailSender;
  27.     private LoggerInterface $logger;
  28.     public function __construct(NotificationEmailSenderInterface $emailSenderLoggerInterface $logger)
  29.     {
  30.         $this->emailSender $emailSender;
  31.         $this->logger $logger;
  32.     }
  33.     #[ArrayShape([CreatedEvent::class => 'string'])]
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CreatedEvent::class => 'onCreated',
  38.         ];
  39.     }
  40.     public function onCreated(CreatedEvent $event): void
  41.     {
  42.         try {
  43.             $this->emailSender->send($event->getNotification());
  44.         } catch (EmptyRecipientEmailExceptionInterface $e) {
  45.             $this->logger->error('Recipient\'s e-mail address is empty.', [
  46.                 'exception' => $e,
  47.             ]);
  48.         }
  49.     }
  50. }