src/FMT/Domain/EventSubscriber/BookstoreApiNotificationSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. namespace FMT\Domain\EventSubscriber;
  3. use Exception;
  4. use FMT\Data\Entity\User;
  5. use FMT\Data\Entity\UserNotifications;
  6. use FMT\Domain\Event\BookstoreApiNotificationEvent;
  7. use FMT\Domain\Repository\UserNotificationsRepositoryInterface;
  8. use FMT\Infrastructure\Helper\NotificationHelper;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Twig\Environment;
  13. class BookstoreApiNotificationSubscriber implements EventSubscriberInterface
  14. {
  15.     private const BOOKSTORE_FAILS_TITLE 'fmt.notifications.api_fails';
  16.     private UserNotificationsRepositoryInterface $userNotificationsRepository;
  17.     private TranslatorInterface $translator;
  18.     private LoggerInterface $logger;
  19.     private Environment $parser;
  20.     public function __construct(
  21.         UserNotificationsRepositoryInterface $userNotificationsRepository,
  22.         TranslatorInterface $translator,
  23.         LoggerInterface $logger,
  24.         Environment $parser
  25.     ) {
  26.         $this->userNotificationsRepository $userNotificationsRepository;
  27.         $this->translator $translator;
  28.         $this->logger $logger;
  29.         $this->parser $parser;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             BookstoreApiNotificationEvent::USERS_NOTIFICATION => 'onBookstoreApiFailed',
  35.             BookstoreApiNotificationEvent::ADMIN_NOTIFICATION => 'notifyAdmin'
  36.         ];
  37.     }
  38.     public function onBookstoreApiFailed(BookstoreApiNotificationEvent $event)
  39.     {
  40.         $users $event->getUsers();
  41.         $message $this->translator->trans(
  42.             'fmt.notifications.api_fails',
  43.             ['%school_name%' => $event->getBookstore()],
  44.         );
  45.         $existingNotifications $this
  46.             ->userNotificationsRepository
  47.             ->findExistingNotificationsForUserByTitle($users$message);
  48.         foreach ($users as $user) {
  49.             if ($this->hasSameNotification($user$message$existingNotifications)) {
  50.                 continue;
  51.             }
  52.             $notification = new UserNotifications();
  53.             $notification->setUser($user);
  54.             $notification->setTitle(BookstoreApiNotificationEvent::NOTIFICATION_TITLE);
  55.             $notification->setMessage($message);
  56.             $this->userNotificationsRepository->add($notification);
  57.         }
  58.         $this->userNotificationsRepository->save();
  59.     }
  60.     public function notifyAdmin(BookstoreApiNotificationEvent $event)
  61.     {
  62.         try {
  63.             $users $event->getUsers();
  64.             foreach ($users as $user) {
  65.                 $message $this->parser->render('@Public/emails/bookstore_blocked.email.html.twig', [
  66.                     'schoolName' => $event->getBookstore(),
  67.                     'user' => $user,
  68.                 ]);
  69.                 NotificationHelper::submitFromTemplate($message$user->getProfile()->getEmail());
  70.             }
  71.         } catch (Exception $exception) {
  72.             $this->logger->error($exception->getMessage());
  73.         }
  74.     }
  75.     /**
  76.      * @param User $user
  77.      * @param string $message
  78.      * @param array $notifications
  79.      * @return bool
  80.      */
  81.     private function hasSameNotification(User $userstring $message, array $notifications): bool
  82.     {
  83.         foreach ($notifications as $notification) {
  84.             if ($notification->getUser() === $user
  85.                 && $notification->isSent() === false
  86.                 && $notification->getTitle() === $message) {
  87.                 return true;
  88.             }
  89.         }
  90.         return false;
  91.     }
  92. }