src/EventSubscriber/MeterReading/MeterReadingSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\MeterReading;
  3. use App\Entity\IndexUpdate;
  4. use App\Manager\ContractManager;
  5. use App\Entity\User;
  6. use App\Event\MeterReading\IndexUpdatedEvent;
  7. use App\EventSubscriber\AbstractEmailSubscriber;
  8. use App\Manager\Mailer;
  9. use App\Manager\MailTypeInterface;
  10. use App\Repository\Scp\ContactRepository;
  11. use App\ScpExport\Strategy\IndexUpdateScpExportStrategy;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Twig\Environment;
  14. class MeterReadingSubscriber extends AbstractEmailSubscriber
  15. {
  16.     private const ENABLE_CENTER_EMAIL false;
  17.     protected $contactRepository;
  18.     protected $exportStrategy;
  19.     protected $contractManager;
  20.     public function __construct(
  21.         Mailer $mailer,
  22.         Environment $templating,
  23.         TranslatorInterface $translator,
  24.         ContactRepository $contactRepository,
  25.         IndexUpdateScpExportStrategy $exportStrategy,
  26.         ContractManager $contractManager
  27.     ) {
  28.         parent::__construct($mailer$templating$translator);
  29.         $this->contactRepository $contactRepository;
  30.         $this->exportStrategy $exportStrategy;
  31.         $this->contractManager $contractManager;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             IndexUpdatedEvent::class => 'onIndexUpdated',
  37.         ];
  38.     }
  39.     public function onIndexUpdated(IndexUpdatedEvent $event): void
  40.     {
  41.         $index $event->getIndex();
  42.         $user $event->getUser();
  43.         $this->sendCustomerEmail($user$index);
  44.         if (self::ENABLE_CENTER_EMAIL && null !== $index->getPicture()) {
  45.             $this->sendCenterEmail($user$index);
  46.         }
  47.     }
  48.     protected function sendCenterEmail(User $userIndexUpdate $index): void
  49.     {
  50.         $technicalContact $this->contactRepository->findOneByDeliveryStationAndType($index->getStationId(), ContactRepository::TYPE_TECHNICAL$index->getCustomerNumber());
  51.         if (null === $technicalContact) {
  52.             return;
  53.         }
  54.         $contract $this->contractManager->getContractByPostNumber($index->getStationId(), $user);
  55.         $reperage $contract['Reperage'];
  56.         $subject $this->transEmail('meter_reading.index_updated_center.subject');
  57.         $message $this->templating->render('email/meter_reading/index_updated_center.html.twig', [
  58.             'customer' => $user,
  59.             'index'    => $index,
  60.             'reperage'    => $reperage,
  61.         ]);
  62.         $picturePath $this->exportStrategy->getExportPath($index).DIRECTORY_SEPARATOR.$index->getPicture();
  63.         $this->mailer->sendMail(
  64.             [$technicalContact['Email']],
  65.             $subject,
  66.             $message,
  67.             MailTypeInterface::METER_READING_INDEX_UPDATED_SCP,
  68.             [\Swift_Attachment::fromPath($picturePath)]
  69.         );
  70.     }
  71.     protected function sendCustomerEmail(User $userIndexUpdate $index): void
  72.     {
  73.         $subject $this->transEmail('meter_reading.index_updated.subject');
  74.         $contract $this->contractManager->getContractByPostNumber($index->getStationId(), $user);
  75.         $reperage $contract['Reperage'];
  76.         $message $this->templating->render('email/meter_reading/index_updated.html.twig', [
  77.             'customer' => $user,
  78.             'index'    => $index,
  79.             'reperage'    => $reperage,
  80.         ]);
  81.         $this->mailer->sendMail([$user->getEmail()], $subject$messageMailTypeInterface::METER_READING_INDEX_UPDATED_USER);
  82.     }
  83. }