src/EventSubscriber/Trackcode/SenderSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Trackcode;
  3. use MLDev\FormBundle\Service\MailerProvider;
  4. use MLDev\OrderBundle\Entity\Order;
  5. use MLDev\OrderBundle\Repository\OrderRepository;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Twig\Environment;
  10. class SenderSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var Request|null
  14.      */
  15.     private $request;
  16.     /**
  17.      * @var Environment
  18.      */
  19.     private $templating;
  20.     /**
  21.      * @var MailerProvider
  22.      */
  23.     private $mailerProvider;
  24.     /**
  25.      * @var OrderRepository
  26.      */
  27.     private $orderRepository;
  28.     public function __construct(
  29.         RequestStack $requestStack,
  30.         Environment $twig,
  31.         MailerProvider $mailerProvider,
  32.         OrderRepository $orderRepository
  33.     ) {
  34.         $this->request $requestStack->getCurrentRequest();
  35.         $this->templating $twig;
  36.         $this->mailerProvider $mailerProvider;
  37.         $this->orderRepository $orderRepository;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             SenderEvent::EVENT_NAME => [
  43.                 ['sendTrackcodeForUser'10]
  44.             ]
  45.         ];
  46.     }
  47.     public function sendTrackcodeForUser(SenderEvent $event): void
  48.     {
  49.         /** @var Order|null $order */
  50.         $order $this->orderRepository->find(
  51.             $event->getTrackcode()->getOrderId()
  52.         );
  53.         if (!$order) {
  54.             return;
  55.         }
  56.         $body $this->templating->render(
  57.             'trackcode/send_trackcode_for_user.html.twig', [
  58.                 'trackcode' => $event->getTrackcode(),
  59.                 'order_id' => $order->getId(),
  60.                 'client_info' => $order->getInfo(),
  61.                 'orderCost' => $order->getOrderCost(),
  62.                 'orderCostWithoutPromocode' => $order->getOrderCost(false),
  63.                 'deliveryInfo' => $order->getDelivery(),
  64.                 'deliveryCost' => $order->getDelivery()->getCost(),
  65.                 'orderItems' => $order->getOrderItems(),
  66.             ]
  67.         );
  68.         $message $this->mailerProvider->createMessage('Ваш трек-номер заказа от Somelove.ru', [
  69.             $event->getTrackcode()->getEmail()
  70.         ]);
  71.         $message->addPart(
  72.             $body,
  73.             'text/html'
  74.         );
  75.         $this->mailerProvider->send($message);
  76.     }
  77. }