src/EventSubscriber/TelegramNotificationSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\TelegramProvider;
  4. use Longman\TelegramBot\Exception\TelegramException;
  5. use MLDev\FormBundle\Event\FormConfirmSuccessEvent;
  6. use MLDev\FormBundle\Service\FormProvider;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use MLDev\OrderBundle\Event\Notification;
  9. use Twig\Environment;
  10. class TelegramNotificationSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var string
  14.      */
  15.     private $chatId;
  16.     /**
  17.      * @var bool
  18.      */
  19.     private $enable;
  20.     /**
  21.      * @var TelegramProvider
  22.      */
  23.     private $telegramProvider;
  24.     /**
  25.      * @var FormProvider
  26.      */
  27.     private $formProvider;
  28.     /**
  29.      * @var Environment
  30.      */
  31.     private $templating;
  32.     /**
  33.      * TelegramNotificationSubscriber constructor.
  34.      *
  35.      * @param bool $enable
  36.      * @param string $chatId
  37.      * @param FormProvider $formProvider
  38.      * @param TelegramProvider $telegramProvider
  39.      * @param Environment $twig
  40.      */
  41.     public function __construct(
  42.         bool $enable,
  43.         string $chatId,
  44.         FormProvider $formProvider,
  45.         TelegramProvider $telegramProvider,
  46.         Environment $twig
  47.     ) {
  48.         $this->enable $enable;
  49.         $this->chatId $chatId;
  50.         $this->templating $twig;
  51.         $this->formProvider $formProvider;
  52.         $this->telegramProvider $telegramProvider;
  53.     }
  54.     public function orderNotificationAfterHandler(Notification $event)
  55.     {
  56.         if (!$this->enable) {
  57.             return;
  58.         }
  59.         $order $event->getOrder();
  60.         $clientInfo $order->getInfo();
  61.         try {
  62.             $this->telegramProvider->sendMessage([
  63.                 'chat_id' => $this->chatId,
  64.                 'text' => $this->templating->render(
  65.                     'telegram_notification/order_notification.html.twig', [
  66.                         'id' => $order->getId(),
  67.                         'clientInfo' => $clientInfo,
  68.                         'paymentInfo' => $order->getPayment(),
  69.                         'deliveryInfo' => $order->getDelivery(),
  70.                         'orderCost' => $order->getOrderCost(),
  71.                         'orderCostOld' => $order->getOrderCost(false),
  72.                         'orderItems' => $order->getOrderItems(),
  73.                         'promocodeAds' => $order->getPromocodeAds()
  74.                     ]
  75.                 ),
  76.             ]);
  77.         } catch (TelegramException $e) {
  78.         }
  79.     }
  80.     public function formConfirmSuccessHandler(FormConfirmSuccessEvent $event)
  81.     {
  82.         if (!$this->enable) {
  83.             return;
  84.         }
  85.         $data $event->getData();
  86.         $name $event->getName();
  87.         list('settings' => $settings) = $this->formProvider->getFormConfigByName($name);
  88.         try {
  89.             $this->telegramProvider->sendMessage([
  90.                 'chat_id' => $this->chatId,
  91.                 'text' => $this->templating->render(
  92.                     'telegram_notification/form_confirm_success.html.twig', [
  93.                         'subject' => $settings['subject'] ?? 'unknown',
  94.                         'formData' => $data,
  95.                     ]
  96.                 ),
  97.             ]);
  98.         } catch (TelegramException $e) {
  99.         }
  100.     }
  101.     public static function getSubscribedEvents(): array
  102.     {
  103.         return [
  104. //            'mldev.order.notification.after' => 'orderNotificationAfterHandler',
  105.             FormConfirmSuccessEvent::EVENT_NAME => 'formConfirmSuccessHandler'
  106.         ];
  107.     }
  108. }