src/EventListener/RequestBaseSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Twig\RequestNotificationAndStatusExtension;
  4. use App\Entity\{NotificationRequestEventsTransactionWallet};
  5. use App\Enum\RequestsNotificationsEnum;
  6. use App\Event\{NotificationEventRequestBaseEventTransactionEvent};
  7. use App\Service\MailerService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  12. use Symfony\Contracts\HttpClient\HttpClientInterface;
  13. class RequestBaseSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityManagerInterface
  17.      */
  18.     private $entityManager;
  19.     /**
  20.      * @var MailerService
  21.      */
  22.     private $mailerService;
  23.     /**
  24.      * @var HttpClientInterface
  25.      */
  26.     private $httpClient;
  27.     /**
  28.      * @var RequestNotificationAndStatusExtension
  29.      */
  30.     private $twigNotifExtension;
  31.     /**
  32.      * @var ParameterBagInterface
  33.      */
  34.     private $parameterBag;
  35.     public function __construct(
  36.         EntityManagerInterface $entityManager,
  37.         MailerService $mailerService,
  38.         HttpClientInterface $client,
  39.         RequestNotificationAndStatusExtension $twigNotifExtension,
  40.         ParameterBagInterface $parameterBag
  41.     ) {
  42.         $this->entityManager $entityManager;
  43.         $this->mailerService $mailerService;
  44.         $this->httpClient $client;
  45.         $this->twigNotifExtension $twigNotifExtension;
  46.         $this->parameterBag $parameterBag;
  47.     }
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             TransactionEvent::class => 'onRequestBaseHandleTransaction',
  52.             NotificationEvent::class => 'onRequestBaseHandleNotification',
  53.             RequestBaseEvent::class => 'onRequestBaseHandleEvent'
  54.         ];
  55.     }
  56.     public function onRequestBaseHandleTransaction(TransactionEvent $event)
  57.     {
  58.         $transaction = new Transaction();
  59.         $transaction
  60.             ->setType($event->type)
  61.             ->setAmount($event->amount)
  62.             ->setStatus($event->status)
  63.             ->setRequestBase($event->requestBase)
  64.             ->setWallet($event->wallet)
  65.             ->setAmountPlatformDescription($event->amountDescription)
  66.             ->setTutorialStudentBackpack($event->tutorialStudentBackpack);
  67.         $this->entityManager->persist($transaction);
  68.     }
  69.     /**
  70.      * @param NotificationEvent $event
  71.      * @throws TransportExceptionInterface
  72.      */
  73.     public function onRequestBaseHandleNotification(NotificationEvent $event)
  74.     {
  75.         // notification
  76.         $notification = new Notification();
  77.         $notification
  78.             ->setType($event->type)
  79.             ->setOwner($event->owner)
  80.             ->setPath($event->path)
  81.             ->setRequestBase($event->requestBase);
  82.         $this->entityManager->persist($notification);
  83.         $this->entityManager->flush();
  84.         $notificationOwnerFirebaseToken $notification->getOwner()->getFirebaseToken();
  85.         if ($notificationOwnerFirebaseToken) {
  86.             $this->httpClient->request(
  87.                 'POST',
  88.                 'https://fcm.googleapis.com/fcm/send',
  89.                 [
  90.                     'headers' => [
  91.                         'Content-Type' => 'application/json',
  92.                         'Authorization' => 'key=' $this->parameterBag->get('firebase_server_key')
  93.                     ],
  94.                     'json' => [
  95.                         'to' => $notificationOwnerFirebaseToken,
  96.                         'notification' => [
  97.                             'title' => 'ESNGP',
  98.                             'body' => $notification->getId() . ' - ' $this->twigNotifExtension->transRequestNotificationStatusLabel(
  99.                                 $notification->getType(),
  100.                                 $notification->getPath()
  101.                             ),
  102.                             'image' => 'https://esngp.school/images/logo_esng.png',
  103.                             'icon' => 'https://esngp.school/images/logo_esng.png',
  104.                             'click_action' => $notification->getPath(),
  105.                         ],
  106.                     ],
  107.                 ]
  108.             );
  109.         }
  110.         $this->mailerService->sendMail(
  111.             $event->tplMailing,
  112.             RequestsNotificationsEnum::$MAP_STATUS[$event->type],
  113.             $event->owner->getEmail(),
  114.             $event->dataMailing,
  115.         );
  116.     }
  117.     public function onRequestBaseHandleEvent(RequestBaseEvent $eventBase)
  118.     {
  119.         $event = new RequestEvents();
  120.         $event->setOwner($eventBase->owner)
  121.             ->setType($eventBase->type)
  122.             ->setRequestBase($eventBase->requestBase);
  123.         $this->entityManager->persist($event);
  124.     }
  125. }