<?php
namespace App\EventSubscriber\ContactSheet;
use App\Event\ContactSheet\AbstractContactSheetEvent;
use App\Event\ContactSheet\ContactSheetCreatedEvent;
use App\Event\ContactSheet\ContactSheetUpdatedEvent;
use App\EventSubscriber\AbstractEmailSubscriber;
use App\Manager\ContactSheetManager;
use App\Manager\Mailer;
use App\Manager\MailTypeInterface;
use App\Repository\Scp\CenterRepository;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class ContactSheetSubscriber extends AbstractEmailSubscriber
{
protected $contactSheetManager;
protected $centerRepository;
protected $mdtEmail;
public function __construct(
Mailer $mailer,
Environment $templating,
TranslatorInterface $translator,
ContactSheetManager $contactSheetManager,
CenterRepository $centerRepository,
string $mdtEmail
) {
parent::__construct($mailer, $templating, $translator);
$this->contactSheetManager = $contactSheetManager;
$this->centerRepository = $centerRepository;
$this->mdtEmail = $mdtEmail;
}
public static function getSubscribedEvents(): array
{
return [
ContactSheetCreatedEvent::class => 'onCreated',
ContactSheetUpdatedEvent::class => 'onUpdated',
];
}
public function onCreated(ContactSheetCreatedEvent $event)
{
$this->sendEmail(
$event,
'contact_sheet.created.subject',
'email/contact_sheet/created.html.twig',
MailTypeInterface::CONTACT_SHEET_CREATED
);
}
public function onUpdated(ContactSheetUpdatedEvent $event)
{
$this->sendEmail(
$event,
'contact_sheet.updated.subject',
'email/contact_sheet/updated.html.twig',
MailTypeInterface::CONTACT_SHEET_UPDATED
);
}
protected function sendEmail(AbstractContactSheetEvent $event, string $subject, string $template, string $mailType): void
{
$contactSheet = $event->getContactSheet();
$user = $event->getUser();
$subject = $this->transEmail($subject);
$message = $this->templating->render($template, [
'customer' => $user,
'contactSheet' => $contactSheet,
]);
$to = [$user->getEmail(), $this->mdtEmail];
$center = $this->centerRepository->findOneByContractNumber($contactSheet->getContractNumber());
if (null !== $center) {
$to[] = $center['Email'];
}
$filePath = $this->contactSheetManager->getFilePath($contactSheet);
$this->mailer->sendMail(
$to,
$subject,
$message,
$mailType,
[\Swift_Attachment::fromPath($filePath)]
);
}
}