src/EventSubscriber/ContactSheet/ContactSheetSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\ContactSheet;
  3. use App\Event\ContactSheet\AbstractContactSheetEvent;
  4. use App\Event\ContactSheet\ContactSheetCreatedEvent;
  5. use App\Event\ContactSheet\ContactSheetUpdatedEvent;
  6. use App\EventSubscriber\AbstractEmailSubscriber;
  7. use App\Manager\ContactSheetManager;
  8. use App\Manager\Mailer;
  9. use App\Manager\MailTypeInterface;
  10. use App\Repository\Scp\CenterRepository;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Twig\Environment;
  13. class ContactSheetSubscriber extends AbstractEmailSubscriber
  14. {
  15.     protected $contactSheetManager;
  16.     protected $centerRepository;
  17.     protected $mdtEmail;
  18.     public function __construct(
  19.         Mailer $mailer,
  20.         Environment $templating,
  21.         TranslatorInterface $translator,
  22.         ContactSheetManager $contactSheetManager,
  23.         CenterRepository $centerRepository,
  24.         string $mdtEmail
  25.     ) {
  26.         parent::__construct($mailer$templating$translator);
  27.         $this->contactSheetManager $contactSheetManager;
  28.         $this->centerRepository $centerRepository;
  29.         $this->mdtEmail $mdtEmail;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ContactSheetCreatedEvent::class => 'onCreated',
  35.             ContactSheetUpdatedEvent::class => 'onUpdated',
  36.         ];
  37.     }
  38.     public function onCreated(ContactSheetCreatedEvent $event)
  39.     {
  40.         $this->sendEmail(
  41.             $event,
  42.             'contact_sheet.created.subject',
  43.             'email/contact_sheet/created.html.twig',
  44.             MailTypeInterface::CONTACT_SHEET_CREATED
  45.         );
  46.     }
  47.     public function onUpdated(ContactSheetUpdatedEvent $event)
  48.     {
  49.         $this->sendEmail(
  50.             $event,
  51.             'contact_sheet.updated.subject',
  52.             'email/contact_sheet/updated.html.twig',
  53.             MailTypeInterface::CONTACT_SHEET_UPDATED
  54.         );
  55.     }
  56.     protected function sendEmail(AbstractContactSheetEvent $eventstring $subjectstring $templatestring $mailType): void
  57.     {
  58.         $contactSheet $event->getContactSheet();
  59.         $user $event->getUser();
  60.         $subject $this->transEmail($subject);
  61.         $message $this->templating->render($template, [
  62.             'customer'     => $user,
  63.             'contactSheet' => $contactSheet,
  64.         ]);
  65.         $to = [$user->getEmail(), $this->mdtEmail];
  66.         $center $this->centerRepository->findOneByContractNumber($contactSheet->getContractNumber());
  67.         if (null !== $center) {
  68.             $to[] = $center['Email'];
  69.         }
  70.         $filePath $this->contactSheetManager->getFilePath($contactSheet);
  71.         $this->mailer->sendMail(
  72.             $to,
  73.             $subject,
  74.             $message,
  75.             $mailType,
  76.             [\Swift_Attachment::fromPath($filePath)]
  77.         );
  78.     }
  79. }