<?php
namespace MLDev\OrderBundle\EventSubscriber;
use App\Service\Bitrix24;
use Exception;
use MLDev\OrderBundle\Event\Notification;
use Swift_Mailer;
use Swift_Message;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/**
* Class NotificationSubscriber
* @package MLDev\OrderBundle\EventSubscriber
*/
class NotificationSubscriber implements EventSubscriberInterface
{
/**
* @var Swift_Mailer
*/
private $mailer;
/**
* @var RequestStack
*/
private $request;
/**
* @var Environment
*/
private $templating;
/**
* @var array
*/
private $orderMail;
/**
* @var Bitrix24
*/
private $bitrix24;
/**
* NotificationSubscriber constructor.
*/
public function __construct(
RequestStack $requestStack,
Swift_Mailer $mailer,
Environment $twig,
Bitrix24 $bitrix24,
$orderMail
) {
$this->mailer = $mailer;
$this->templating = $twig;
$this->bitrix24 = $bitrix24;
$this->request = $requestStack->getCurrentRequest();
$this->orderMail = $orderMail;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
Notification::EVENT_AFTER => [
['notifyUser', 20],
['notifyAdmin', 10],
// ['notifyBitrix24', 10]
]
];
}
/**
* @param Notification $event
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function notifyUser(Notification $event)
{
$mldevOrderMail = $this->orderMail;
$order = $event->getOrder();
$clientInfo = $order->getInfo();
if (filter_var($clientInfo->getEmail(), FILTER_VALIDATE_EMAIL)) {
$message = new Swift_Message(
'Ваш заказ №' . $order->getId() . ' принят'
);
$message->setFrom($mldevOrderMail['from']);
$message->setTo($clientInfo->getEmail());
$message->setBody(
$this->templating->render(
'@MLDevOrder/notification/user/letter.html.twig', [
'id' => $order->getId(),
'clientInfo' => $clientInfo,
'orderCost' => $order->getOrderCost(),
'orderCostWithoutPromocode' => $order->getOrderCost(false),
'deliveryInfo' => $order->getDelivery(),
'deliveryCost' => $order->getDelivery()->getCost(),
'orderItems' => $order->getOrderItems(),
]
), 'text/html'
);
$this->mailer->send($message);
}
}
/**
* @param Notification $event
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function notifyAdmin(Notification $event)
{
$mldevOrderMail = $this->orderMail;
$order = $event->getOrder();
$clientInfo = $order->getInfo();
$message = new Swift_Message(
'Заказ №' . $order->getId() . ' с сайта somelove.ru'
);
$message->setFrom($mldevOrderMail['from']);
$message->setTo($mldevOrderMail['to']);
$message->setBody(
$this->templating->render(
'@MLDevOrder/notification/admin/letter.html.twig', [
'id' => $order->getId(),
'clientInfo' => $clientInfo,
'deliveryInfo' => $order->getDelivery(),
'orderCost' => $order->getOrderCost(),
'orderItems' => $order->getOrderItems(),
]
), 'text/html'
);
$this->mailer->send($message);
}
// public function notifyBitrix24(Notification $event)
// {
// $order = $event->getOrder();
//
// try {
// $this->bitrix24->addOrderInBitrix($order);
// } catch (Exception $exception) {
//
// }
// }
}