<?php
namespace App\EventListener;
use App\Twig\RequestNotificationAndStatusExtension;
use App\Entity\{Notification, RequestEvents, Transaction, Wallet};
use App\Enum\RequestsNotificationsEnum;
use App\Event\{NotificationEvent, RequestBaseEvent, TransactionEvent};
use App\Service\MailerService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class RequestBaseSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var MailerService
*/
private $mailerService;
/**
* @var HttpClientInterface
*/
private $httpClient;
/**
* @var RequestNotificationAndStatusExtension
*/
private $twigNotifExtension;
/**
* @var ParameterBagInterface
*/
private $parameterBag;
public function __construct(
EntityManagerInterface $entityManager,
MailerService $mailerService,
HttpClientInterface $client,
RequestNotificationAndStatusExtension $twigNotifExtension,
ParameterBagInterface $parameterBag
) {
$this->entityManager = $entityManager;
$this->mailerService = $mailerService;
$this->httpClient = $client;
$this->twigNotifExtension = $twigNotifExtension;
$this->parameterBag = $parameterBag;
}
public static function getSubscribedEvents()
{
return [
TransactionEvent::class => 'onRequestBaseHandleTransaction',
NotificationEvent::class => 'onRequestBaseHandleNotification',
RequestBaseEvent::class => 'onRequestBaseHandleEvent'
];
}
public function onRequestBaseHandleTransaction(TransactionEvent $event)
{
$transaction = new Transaction();
$transaction
->setType($event->type)
->setAmount($event->amount)
->setStatus($event->status)
->setRequestBase($event->requestBase)
->setWallet($event->wallet)
->setAmountPlatformDescription($event->amountDescription)
->setTutorialStudentBackpack($event->tutorialStudentBackpack);
$this->entityManager->persist($transaction);
}
/**
* @param NotificationEvent $event
* @throws TransportExceptionInterface
*/
public function onRequestBaseHandleNotification(NotificationEvent $event)
{
// notification
$notification = new Notification();
$notification
->setType($event->type)
->setOwner($event->owner)
->setPath($event->path)
->setRequestBase($event->requestBase);
$this->entityManager->persist($notification);
$this->entityManager->flush();
$notificationOwnerFirebaseToken = $notification->getOwner()->getFirebaseToken();
if ($notificationOwnerFirebaseToken) {
$this->httpClient->request(
'POST',
'https://fcm.googleapis.com/fcm/send',
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'key=' . $this->parameterBag->get('firebase_server_key')
],
'json' => [
'to' => $notificationOwnerFirebaseToken,
'notification' => [
'title' => 'ESNGP',
'body' => $notification->getId() . ' - ' . $this->twigNotifExtension->transRequestNotificationStatusLabel(
$notification->getType(),
$notification->getPath()
),
'image' => 'https://esngp.school/images/logo_esng.png',
'icon' => 'https://esngp.school/images/logo_esng.png',
'click_action' => $notification->getPath(),
],
],
]
);
}
$this->mailerService->sendMail(
$event->tplMailing,
RequestsNotificationsEnum::$MAP_STATUS[$event->type],
$event->owner->getEmail(),
$event->dataMailing,
);
}
public function onRequestBaseHandleEvent(RequestBaseEvent $eventBase)
{
$event = new RequestEvents();
$event->setOwner($eventBase->owner)
->setType($eventBase->type)
->setRequestBase($eventBase->requestBase);
$this->entityManager->persist($event);
}
}