bundles/mldev-order-bundle/src/EventSubscriber/NotificationSubscriber.php line 126

Open in your IDE?
  1. <?php
  2. namespace MLDev\OrderBundle\EventSubscriber;
  3. use App\Service\Bitrix24;
  4. use Exception;
  5. use MLDev\OrderBundle\Event\Notification;
  6. use Swift_Mailer;
  7. use Swift_Message;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Twig\Environment;
  11. use Twig\Error\LoaderError;
  12. use Twig\Error\RuntimeError;
  13. use Twig\Error\SyntaxError;
  14. /**
  15.  * Class NotificationSubscriber
  16.  * @package MLDev\OrderBundle\EventSubscriber
  17.  */
  18. class NotificationSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var Swift_Mailer
  22.      */
  23.     private $mailer;
  24.     /**
  25.      * @var RequestStack
  26.      */
  27.     private $request;
  28.     /**
  29.      * @var Environment
  30.      */
  31.     private $templating;
  32.     /**
  33.      * @var array
  34.      */
  35.     private $orderMail;
  36.     /**
  37.      * @var Bitrix24
  38.      */
  39.     private $bitrix24;
  40.     /**
  41.      * NotificationSubscriber constructor.
  42.      */
  43.     public function __construct(
  44.         RequestStack $requestStack,
  45.         Swift_Mailer $mailer,
  46.         Environment $twig,
  47.         Bitrix24 $bitrix24,
  48.         $orderMail
  49.     ) {
  50.         $this->mailer $mailer;
  51.         $this->templating $twig;
  52.         $this->bitrix24 $bitrix24;
  53.         $this->request $requestStack->getCurrentRequest();
  54.         $this->orderMail $orderMail;
  55.     }
  56.     /**
  57.      * @return array
  58.      */
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             Notification::EVENT_AFTER => [
  63.                 ['notifyUser'20],
  64.                 ['notifyAdmin'10],
  65. //                ['notifyBitrix24', 10]
  66.             ]
  67.         ];
  68.     }
  69.     /**
  70.      * @param Notification $event
  71.      * @throws LoaderError
  72.      * @throws RuntimeError
  73.      * @throws SyntaxError
  74.      */
  75.     public function notifyUser(Notification $event)
  76.     {
  77.         $mldevOrderMail $this->orderMail;
  78.         $order $event->getOrder();
  79.         $clientInfo $order->getInfo();
  80.         if (filter_var($clientInfo->getEmail(), FILTER_VALIDATE_EMAIL)) {
  81.             $message = new Swift_Message(
  82.                 'Ваш заказ №' $order->getId() . ' принят'
  83.             );
  84.             $message->setFrom($mldevOrderMail['from']);
  85.             $message->setTo($clientInfo->getEmail());
  86.             $message->setBody(
  87.                 $this->templating->render(
  88.                     '@MLDevOrder/notification/user/letter.html.twig', [
  89.                         'id' => $order->getId(),
  90.                         'clientInfo' => $clientInfo,
  91.                         'orderCost' => $order->getOrderCost(),
  92.                         'orderCostWithoutPromocode' => $order->getOrderCost(false),
  93.                         'deliveryInfo' => $order->getDelivery(),
  94.                         'deliveryCost' => $order->getDelivery()->getCost(),
  95.                         'orderItems' => $order->getOrderItems(),
  96.                     ]
  97.                 ), 'text/html'
  98.             );
  99.             $this->mailer->send($message);
  100.         }
  101.     }
  102.     /**
  103.      * @param Notification $event
  104.      * @throws LoaderError
  105.      * @throws RuntimeError
  106.      * @throws SyntaxError
  107.      */
  108.     public function notifyAdmin(Notification $event)
  109.     {
  110.         $mldevOrderMail $this->orderMail;
  111.         $order $event->getOrder();
  112.         $clientInfo $order->getInfo();
  113.         $message = new Swift_Message(
  114.             'Заказ №' $order->getId() . ' с сайта somelove.ru'
  115.         );
  116.         $message->setFrom($mldevOrderMail['from']);
  117.         $message->setTo($mldevOrderMail['to']);
  118.         $message->setBody(
  119.             $this->templating->render(
  120.                 '@MLDevOrder/notification/admin/letter.html.twig', [
  121.                     'id' => $order->getId(),
  122.                     'clientInfo' => $clientInfo,
  123.                     'deliveryInfo' => $order->getDelivery(),
  124.                     'orderCost' => $order->getOrderCost(),
  125.                     'orderItems' => $order->getOrderItems(),
  126.                 ]
  127.             ), 'text/html'
  128.         );
  129.         $this->mailer->send($message);
  130.     }
  131. //    public function notifyBitrix24(Notification $event)
  132. //    {
  133. //        $order = $event->getOrder();
  134. //
  135. //        try {
  136. //            $this->bitrix24->addOrderInBitrix($order);
  137. //        } catch (Exception $exception) {
  138. //
  139. //        }
  140. //    }
  141. }