<?php
namespace App\EventSubscriber\Contact;
use App\Entity\ContactSheet\ContactSheet;
use App\Entity\User;
use App\Event\Contact\ContactUpdatedEvent;
use App\EventSubscriber\AbstractEmailSubscriber;
use App\Manager\ContactSheetManager;
use App\Manager\Mailer;
use App\Manager\MailTypeInterface;
use App\Repository\App\ContactSheet\ContactSheetRepository;
use App\Repository\Scp\CenterRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class ContactSubscriber extends AbstractEmailSubscriber implements EventSubscriberInterface
{
protected $contactSheetManager;
protected $contactSheetRepository;
protected $centerRepository;
protected $mdtEmail;
public function __construct(
Mailer $mailer,
Environment $templating,
TranslatorInterface $translator,
ContactSheetManager $contactSheetManager,
ContactSheetRepository $contactSheetRepository,
CenterRepository $centerRepository,
string $mdtEmail
) {
parent::__construct($mailer, $templating, $translator);
$this->contactSheetManager = $contactSheetManager;
$this->contactSheetRepository = $contactSheetRepository;
$this->centerRepository = $centerRepository;
$this->mdtEmail = $mdtEmail;
}
public static function getSubscribedEvents(): array
{
return [
ContactUpdatedEvent::class => 'onUpdated',
];
}
public function onUpdated(ContactUpdatedEvent $event)
{
$contact = $event->getUser();
$mainAccount = $contact->getMainAccount();
$contactSheets = $this->contactSheetRepository->findAllByUserId($contact->getId());
foreach ($contactSheets as $contactSheet){
$this->contactSheetManager->generateFile($contactSheet, $mainAccount);
$this->sendEmail(
$contactSheet,
$mainAccount,
'contact_sheet.updated.subject',
'email/contact_sheet/updated.html.twig',
MailTypeInterface::CONTACT_SHEET_CREATED
);
}
}
protected function sendEmail(ContactSheet $contactSheet, User $user, string $subject, string $template, string $mailType): void
{
$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)]
);
}
}