<?php
namespace Boab\EcommerceBundle\EventSubscriber;
use Boab\EcommerceBundle\Event\PaymentCompletedEvent;
use Boab\EcommerceBundle\Events;
use Boab\EcommerceBundle\Manager\OrderManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class EmailMessengerSubscriber implements EventSubscriberInterface
{
private $mailer;
private $router;
private $orderManager;
public function __construct(OrderManagerInterface $orderManager, RouterInterface $router, MailerInterface $mailer)
{
$this->mailer = $mailer;
$this->router = $router;
$this->orderManager = $orderManager;
}
public function onPaymentSuccess(PaymentCompletedEvent $event)
{
$order = $event->getOrder();
if(!$order->isPaid()){
return;
}
$downloadUrl = $this->router->generate('app_download_index',
['orderId'=>$order->getOrderId(), 'file'=>$order->getHash()],
UrlGeneratorInterface::ABSOLUTE_URL
);
$this->orderManager->setOrder($order);
$email = (new TemplatedEmail())
->from(new Address('mailer@bomateq.com', 'PIS Entertainment'))
->to($order->getEmail())
->subject('Purchase Successful')
->htmlTemplate('email/order_detail.html.twig')
->context([
'downloadUrl' => $downloadUrl,
'order' => $order,
"charges" => $this->orderManager->getTaxCharges(),
"grand_total" => $this->orderManager->getGrandTotal(),
]);
$this->mailer->send($email);
}
public function onSendDownloadLink($order)
{
$downloadUrl = $this->router->generate('app_download_index',
['orderId'=>$order->getOrderId(), 'file'=>$order->getHash()],
UrlGeneratorInterface::ABSOLUTE_URL
);
$email = (new TemplatedEmail())
->from(new Address('mailer@bomateq.com', 'PIS Entertainment'))
->to($order->getEmail())
->subject('Download your files')
->htmlTemplate('email/order_detail.html.twig')
->context([
'downloadUrl' => $downloadUrl,
'order' => $order
]);
$this->mailer->send($email);
}
public static function getSubscribedEvents():array
{
return [
Events::PAYMENT_SUCCESS => 'onPaymentSuccess',
Events::SEND_DOWNLOAD_LINK => 'onSendDownloadLink'
];
}
}