<?php
namespace App\EventSubscriber\Trackcode;
use MLDev\FormBundle\Service\MailerProvider;
use MLDev\OrderBundle\Entity\Order;
use MLDev\OrderBundle\Repository\OrderRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
class SenderSubscriber implements EventSubscriberInterface
{
/**
* @var Request|null
*/
private $request;
/**
* @var Environment
*/
private $templating;
/**
* @var MailerProvider
*/
private $mailerProvider;
/**
* @var OrderRepository
*/
private $orderRepository;
public function __construct(
RequestStack $requestStack,
Environment $twig,
MailerProvider $mailerProvider,
OrderRepository $orderRepository
) {
$this->request = $requestStack->getCurrentRequest();
$this->templating = $twig;
$this->mailerProvider = $mailerProvider;
$this->orderRepository = $orderRepository;
}
public static function getSubscribedEvents(): array
{
return [
SenderEvent::EVENT_NAME => [
['sendTrackcodeForUser', 10]
]
];
}
public function sendTrackcodeForUser(SenderEvent $event): void
{
/** @var Order|null $order */
$order = $this->orderRepository->find(
$event->getTrackcode()->getOrderId()
);
if (!$order) {
return;
}
$body = $this->templating->render(
'trackcode/send_trackcode_for_user.html.twig', [
'trackcode' => $event->getTrackcode(),
'order_id' => $order->getId(),
'client_info' => $order->getInfo(),
'orderCost' => $order->getOrderCost(),
'orderCostWithoutPromocode' => $order->getOrderCost(false),
'deliveryInfo' => $order->getDelivery(),
'deliveryCost' => $order->getDelivery()->getCost(),
'orderItems' => $order->getOrderItems(),
]
);
$message = $this->mailerProvider->createMessage('Ваш трек-номер заказа от Somelove.ru', [
$event->getTrackcode()->getEmail()
]);
$message->addPart(
$body,
'text/html'
);
$this->mailerProvider->send($message);
}
}