lib/boab/ecommerce-bundle/src/EventSubscriber/EmailMessengerSubscriber.php line 56

Open in your IDE?
  1. <?php 
  2. namespace Boab\EcommerceBundle\EventSubscriber;
  3. use Boab\EcommerceBundle\Event\PaymentCompletedEvent;
  4. use Boab\EcommerceBundle\Events;
  5. use Boab\EcommerceBundle\Manager\OrderManagerInterface;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Mime\Address;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. class EmailMessengerSubscriber implements EventSubscriberInterface
  13. {
  14.     private $mailer;
  15.     private $router;
  16.     private $orderManager;
  17.     public function __construct(OrderManagerInterface $orderManagerRouterInterface $routerMailerInterface $mailer)
  18.     {
  19.         $this->mailer $mailer;
  20.         $this->router $router;
  21.         $this->orderManager $orderManager;
  22.     }
  23.     public function onPaymentSuccess(PaymentCompletedEvent $event)
  24.     {
  25.         $order $event->getOrder();
  26.         if(!$order->isPaid()){
  27.             return;
  28.         }
  29.         $downloadUrl $this->router->generate('app_download_index'
  30.             ['orderId'=>$order->getOrderId(), 'file'=>$order->getHash()], 
  31.             UrlGeneratorInterface::ABSOLUTE_URL
  32.         );
  33.         
  34.         $this->orderManager->setOrder($order);
  35.         
  36.         $email = (new TemplatedEmail())
  37.             ->from(new Address('mailer@bomateq.com''PIS Entertainment'))
  38.             ->to($order->getEmail())
  39.             ->subject('Purchase Successful')
  40.             ->htmlTemplate('email/order_detail.html.twig')
  41.             ->context([
  42.                 'downloadUrl' => $downloadUrl,
  43.                 'order' => $order,
  44.                 "charges" => $this->orderManager->getTaxCharges(),
  45.                 "grand_total" => $this->orderManager->getGrandTotal(),                
  46.             ]);
  47.         
  48.         $this->mailer->send($email);
  49.     }
  50.     public function onSendDownloadLink($order)
  51.     {
  52.         $downloadUrl $this->router->generate('app_download_index'
  53.             ['orderId'=>$order->getOrderId(), 'file'=>$order->getHash()], 
  54.             UrlGeneratorInterface::ABSOLUTE_URL
  55.         );
  56.         $email = (new TemplatedEmail())
  57.             ->from(new Address('mailer@bomateq.com''PIS Entertainment'))
  58.             ->to($order->getEmail())
  59.             ->subject('Download your files')
  60.             ->htmlTemplate('email/order_detail.html.twig')
  61.             ->context([
  62.                 'downloadUrl' => $downloadUrl,
  63.                 'order' => $order
  64.             ]);
  65.         
  66.         $this->mailer->send($email);
  67.     }
  68.     public static function getSubscribedEvents():array
  69.     {
  70.         return [
  71.             Events::PAYMENT_SUCCESS => 'onPaymentSuccess',
  72.             Events::SEND_DOWNLOAD_LINK => 'onSendDownloadLink'
  73.         ];
  74.     }  
  75. }