<?php
namespace Suzuki\RestBundle\EventDispatcher\Listener;
use Exception;
use Suzuki\CoreBundle\Helpers\PdfHelper;
use Suzuki\RestBundle\EventDispatcher\Event\SavedConfiguration;
use Swift_Attachment;
use Swift_Mailer;
use Swift_Message;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/**
* Class SavedConfigurationListener
*
* @package Suzuki\RestBundle\EventDispatcher\Listener
*/
class SavedConfigurationListener
{
/**
* @var PdfHelper
*/
protected $pdfHelper;
/**
* @var Swift_Mailer
*/
private $mailer;
/**
* @var Environment
*/
private $templating;
/**
* @var ParameterBagInterface
*/
private $parameterBag;
/**
* @var $mailAdmin
*/
private $mailFrom;
/**
* @var mixed
*/
private $mailSubject;
/**
* SavedConfigurationListener constructor.
*
* @param Swift_Mailer $mailer
* @param Environment $templating
* @param ParameterBagInterface $parameterBag
* @param PdfHelper $pdfHelper
*/
public function __construct(
Swift_Mailer $mailer,
Environment $templating,
ParameterBagInterface $parameterBag,
PdfHelper $pdfHelper
) {
$this->parameterBag = $parameterBag;
$this->mailer = $mailer;
$this->templating = $templating;
$this->mailFrom = $this->parameterBag->get('MAILER_FROM');
$this->mailSubject = $this->parameterBag->get('MAILER_SUBJECT');
$this->pdfHelper = $pdfHelper;
}
/**
* @param SavedConfiguration $event
*
* @throws Exception
*/
public function onSavedConfiguration(SavedConfiguration $event)
: void {
try {
$template = '@Rest/pdf_template.html.twig';
$pdfDirection = $this->pdfHelper->generationPDF($template, $event->getTemplateData());
$this->sendEmailActionPDFAttachment($event->getData(), $event->getReceiver(), $pdfDirection);
} catch (Exception $e) {
throw $e;
}
}
/**
* @param $data
* @param $sendToEmail
* @param $pdfAttachment
*
* @return int
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
private function sendEmailActionPDFAttachment($data, $sendToEmail, $pdfAttachment)
{
$message = (new Swift_Message())
->setSubject($this->mailSubject)
->setFrom($this->mailFrom)
->setTo($sendToEmail)
->setBody(
$this->templating->render(
'@Rest/mail_template.html.twig',
[
'data' => $data,
]
),
'text/html'
);
$filename = 'configuration_summary_' . $data['code'] . '.pdf';
$attachment = (new Swift_Attachment())
->setBody($pdfAttachment)
->setContentType('application/pdf')
->setFilename($filename);
$message->attach($attachment);
return $this->mailer->send($message);
}
/**
* @param $data
* @param $sendToEmail
* @param $attachmentPath
*
* @return int
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
private function sendEmailActionAttachPathFile($data, $sendToEmail, $attachmentPath)
{
$message = (new Swift_Message())
->setSubject($this->mailSubject)
->setFrom($this->mailFrom)
->setTo($sendToEmail)
->setBody(
$this->templating->render(
'@Rest/mail_template.html.twig',
[
'data' => $data,
]
),
'text/html'
);
$message->attach(Swift_Attachment::fromPath($attachmentPath));
return $this->mailer->send($message);
}
}