bundles/Suzuki/RestBundle/EventDispatcher/Listener/SavedConfigurationListener.php line 81

Open in your IDE?
  1. <?php
  2. namespace Suzuki\RestBundle\EventDispatcher\Listener;
  3. use Exception;
  4. use Suzuki\CoreBundle\Helpers\PdfHelper;
  5. use Suzuki\RestBundle\EventDispatcher\Event\SavedConfiguration;
  6. use Swift_Attachment;
  7. use Swift_Mailer;
  8. use Swift_Message;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Twig\Environment;
  11. use Twig\Error\LoaderError;
  12. use Twig\Error\RuntimeError;
  13. use Twig\Error\SyntaxError;
  14. /**
  15.  * Class SavedConfigurationListener
  16.  *
  17.  * @package Suzuki\RestBundle\EventDispatcher\Listener
  18.  */
  19. class SavedConfigurationListener
  20. {
  21.     /**
  22.      * @var PdfHelper
  23.      */
  24.     protected $pdfHelper;
  25.     /**
  26.      * @var Swift_Mailer
  27.      */
  28.     private $mailer;
  29.     /**
  30.      * @var Environment
  31.      */
  32.     private $templating;
  33.     /**
  34.      * @var ParameterBagInterface
  35.      */
  36.     private $parameterBag;
  37.     /**
  38.      * @var $mailAdmin
  39.      */
  40.     private $mailFrom;
  41.     /**
  42.      * @var mixed
  43.      */
  44.     private $mailSubject;
  45.     /**
  46.      * SavedConfigurationListener constructor.
  47.      *
  48.      * @param Swift_Mailer          $mailer
  49.      * @param Environment           $templating
  50.      * @param ParameterBagInterface $parameterBag
  51.      * @param PdfHelper             $pdfHelper
  52.      */
  53.     public function __construct(
  54.         Swift_Mailer $mailer,
  55.         Environment $templating,
  56.         ParameterBagInterface $parameterBag,
  57.         PdfHelper $pdfHelper
  58.     ) {
  59.         $this->parameterBag $parameterBag;
  60.         $this->mailer       $mailer;
  61.         $this->templating   $templating;
  62.         $this->mailFrom    $this->parameterBag->get('MAILER_FROM');
  63.         $this->mailSubject  $this->parameterBag->get('MAILER_SUBJECT');
  64.         $this->pdfHelper    $pdfHelper;
  65.     }
  66.     /**
  67.      * @param SavedConfiguration $event
  68.      *
  69.      * @throws Exception
  70.      */
  71.     public function onSavedConfiguration(SavedConfiguration $event)
  72.     : void {
  73.         try {
  74.             $template     '@Rest/pdf_template.html.twig';
  75.             $pdfDirection $this->pdfHelper->generationPDF($template$event->getTemplateData());
  76.             $this->sendEmailActionPDFAttachment($event->getData(), $event->getReceiver(), $pdfDirection);
  77.         } catch (Exception $e) {
  78.             throw $e;
  79.         }
  80.     }
  81.     /**
  82.      * @param $data
  83.      * @param $sendToEmail
  84.      * @param $pdfAttachment
  85.      *
  86.      * @return int
  87.      * @throws LoaderError
  88.      * @throws RuntimeError
  89.      * @throws SyntaxError
  90.      */
  91.     private function sendEmailActionPDFAttachment($data$sendToEmail$pdfAttachment)
  92.     {
  93.         $message    = (new Swift_Message())
  94.             ->setSubject($this->mailSubject)
  95.             ->setFrom($this->mailFrom)
  96.             ->setTo($sendToEmail)
  97.             ->setBody(
  98.                 $this->templating->render(
  99.                     '@Rest/mail_template.html.twig',
  100.                     [
  101.                         'data' => $data,
  102.                     ]
  103.                 ),
  104.                 'text/html'
  105.             );
  106.         $filename   'configuration_summary_' $data['code'] . '.pdf';
  107.         $attachment = (new Swift_Attachment())
  108.             ->setBody($pdfAttachment)
  109.             ->setContentType('application/pdf')
  110.             ->setFilename($filename);
  111.         $message->attach($attachment);
  112.         return $this->mailer->send($message);
  113.     }
  114.     /**
  115.      * @param $data
  116.      * @param $sendToEmail
  117.      * @param $attachmentPath
  118.      *
  119.      * @return int
  120.      * @throws LoaderError
  121.      * @throws RuntimeError
  122.      * @throws SyntaxError
  123.      */
  124.     private function sendEmailActionAttachPathFile($data$sendToEmail$attachmentPath)
  125.     {
  126.         $message = (new Swift_Message())
  127.             ->setSubject($this->mailSubject)
  128.             ->setFrom($this->mailFrom)
  129.             ->setTo($sendToEmail)
  130.             ->setBody(
  131.                 $this->templating->render(
  132.                     '@Rest/mail_template.html.twig',
  133.                     [
  134.                         'data' => $data,
  135.                     ]
  136.                 ),
  137.                 'text/html'
  138.             );
  139.         $message->attach(Swift_Attachment::fromPath($attachmentPath));
  140.         return $this->mailer->send($message);
  141.     }
  142. }