src/Aqarmap/Bundle/NotificationBundle/NotificationSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\NotificationBundle;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Twig\Environment;
  6. class NotificationSubscriber implements EventSubscriberInterface
  7. {
  8.     public NotificationManager $manager;
  9.     /**
  10.      * [$action description].
  11.      *
  12.      * @var string
  13.      */
  14.     public $action;
  15.     private Environment $templating;
  16.     /**
  17.      * Create a notification subscriber instance.
  18.      */
  19.     public function __construct(NotificationManager $managerNotificationLogger $loggerEnvironment $templating)
  20.     {
  21.         $this->manager $manager;
  22.         $this->logger $logger;
  23.         $this->templating $templating;
  24.     }
  25.     /**
  26.      * Register the listeners for the subscriber.
  27.      */
  28.     public static function getSubScribedEvents()
  29.     {
  30.         $path __DIR__.'/Types';
  31.         $events = [];
  32.         $AllFileIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
  33.         $PhpFileIterator = new \RegexIterator($AllFileIterator'/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
  34.         foreach ($PhpFileIterator as $file => $object) {
  35.             $file pathinfo($file);
  36.             $event strtolower(preg_replace('~(?<=\\w)([A-Z])~''.$1'$file['filename']));
  37.             $events[$event] = 'handle';
  38.         }
  39.         return $events;
  40.     }
  41.     /**
  42.      * Handel notification.
  43.      *
  44.      * @throws \ReflectionException
  45.      */
  46.     public function handle(NotificationInterface $notification): void
  47.     {
  48.         $notification
  49.             ->manager($this->manager)
  50.             ->twig($this->templating)
  51.             ->translator($this->manager->get('translator'));
  52.         // force sending a particular channel
  53.         if ($notification->isForce) {
  54.             $notificationHandler $this->manager->getHandler($notification);
  55.             if (
  56.                 !($notification->forceSend ?? false)
  57.                 && $notificationHandler
  58.                 && method_exists($notification$notificationHandler)
  59.             ) {
  60.                 $notification->$notificationHandler();
  61.                 return;
  62.             }
  63.             $this->send($notification$notification->channel);
  64.             return;
  65.         }
  66.         if ($notification->getChannel()) {
  67.             $this->hasChannel($notification$notification->getChannel())
  68.                 ? $this->queue($notification$notification->getChannel())
  69.                 : $this->send($notification$notification->getChannel());
  70.             return;
  71.         }
  72.         foreach ($this->manager->getChannel($notification) as $channel) {
  73.             $this->hasChannel($notification$channel)
  74.                 ? $this->queue($notification$channel)
  75.                 : $this->send($notification$channel);
  76.         }
  77.     }
  78.     /**
  79.      * Send notification.
  80.      *
  81.      * @param string $channel
  82.      *
  83.      * @throws Exceptions\DriverNotFoundException
  84.      */
  85.     public function send(NotificationInterface $notification$channel): void
  86.     {
  87.         $notifiables $notification->getNotifiables();
  88.         if (!\count($notifiables)) {
  89.             $notifiables $notification->getUsers();
  90.         }
  91.         foreach ($notifiables as $user) {
  92.             if (!$user->isEnabled()) {
  93.                 continue;
  94.             }
  95.             $user $notification->to($user);
  96.             $this->manager->send($user$notification$channel);
  97.         }
  98.         /** @var EntityManagerInterface $em */
  99.         $em $this->manager->get('doctrine')->getManager();
  100.         $em->flush();
  101.     }
  102.     /**
  103.      * Queue notification.
  104.      *
  105.      * @param string $channel
  106.      *
  107.      * @throws \ReflectionException
  108.      */
  109.     public function queue(NotificationInterface $notification$channel): void
  110.     {
  111.         $message = [
  112.             'event' => strtolower(
  113.                 preg_replace(
  114.                     '~(?<=\\w)([A-Z])~',
  115.                     '.$1',
  116.                     (new \ReflectionClass($notification))->getShortName()
  117.                 )
  118.             ),
  119.             'class' => (new \ReflectionClass($notification))->getName(),
  120.             'entity' => (new \ReflectionClass($notification->getSubject()))->getName(),
  121.             'id' => $notification->getSubject()->getId(),
  122.             'channel' => $channel,
  123.             'sent_at' => date('d-m-Y \@ h:i A'),
  124.         ];
  125.         $this->manager->queue(json_encode($message), $notification->getHandoverDetails());
  126.     }
  127.     /**
  128.      * Check that notification has channel.
  129.      *
  130.      * @throws \ReflectionException
  131.      */
  132.     public function hasChannel(NotificationInterface $notification$channel): bool
  133.     {
  134.         return \in_array($channel$this->manager->getQueue($notification));
  135.     }
  136. }