src/Infrastructure/Notifications/EventSubscriber/NotificationSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Infrastructure\Notifications\EventSubscriber;
  4. use App\Infrastructure\Notifications\Event\CreateNotificationEvent;
  5. use App\Infrastructure\Notifications\Event\FireBaseNotificationSendEvent;
  6. use DateTimeImmutable;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Kreait\Firebase\Contract\Messaging;
  9. use Kreait\Firebase\Exception\FirebaseException;
  10. use Kreait\Firebase\Exception\MessagingException;
  11. use Kreait\Firebase\Messaging\CloudMessage;
  12. use Kreait\Firebase\Messaging\WebPushConfig as FireBaseWebPush;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class NotificationSubscriber implements EventSubscriberInterface
  15. {
  16.     private Messaging $messaging;
  17.     private EntityManagerInterface $entityManager;
  18.     
  19.     public function __construct(
  20.        // Messaging $messaging,
  21.         EntityManagerInterface $entityManager
  22.     )
  23.     {
  24.   //      $this->messaging = $messaging;
  25.         $this->entityManager $entityManager;
  26.     }
  27.     
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             //Создание уведомления в бд
  32.             CreateNotificationEvent::class       => 'onNotificationCreated',
  33.             //Отправка уведомления
  34.             FireBaseNotificationSendEvent::class => 'onNotificationSendToFirebase',
  35.         ];
  36.     }
  37.     
  38.     /**
  39.      * @throws MessagingException
  40.      * @throws FirebaseException
  41.      */
  42.     public function onNotificationSendToFirebase(FireBaseNotificationSendEvent $notificationSendEvent): void
  43.     {
  44.         $notification $notificationSendEvent->getNotification();
  45.         
  46.         //Если еще не отправлено и у пользователя есть токен
  47.         if($notification->getPostedAt() === null && $notification->getUser()->getNotificationToken() !== null){
  48.             $message CloudMessage::withTarget('token'$notification->getUser()->getNotificationToken())
  49.                 ->withWebPushConfig(
  50.                     FireBaseWebPush::fromArray([
  51.                             'notification' => [
  52.                                 'title' => $notification->getTitle(),
  53.                                 'body' => $notification->getDescription(),
  54.                                 'icon' => '',
  55.                             ],
  56.                             'fcm_options' => [
  57.                                 'link' => $notification->getLink(),
  58.                             ]
  59.                     ])
  60.                 )
  61.                 ->withData([$notification->getAdditionalData()]);
  62.     
  63.             //$this->messaging->send($message);
  64.         }
  65.         $notification->setPostedAt(new DateTimeImmutable());
  66.         $this->entityManager->flush();
  67.     }
  68.     
  69.     public function onNotificationCreated(FireBaseNotificationSendEvent $notificationSendEvent): void
  70.     {
  71.         $notification $notificationSendEvent->getNotification();
  72.         
  73.         $this->entityManager->persist($notification);
  74.         $this->entityManager->flush();
  75.     }
  76. }