src/Api/EventListener/ImportAlertListener.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Api\EventListener;
  3. use App\Api\Event\ImportEvent;
  4. use App\Entity\User;
  5. use App\Entity\Vehicle;
  6. use App\Entity\VehicleAlert;
  7. use App\Mailer\TranslatableMailer;
  8. use Doctrine\ORM\EntityManager;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\Mailer\Exception\TransportException;
  11. use Twig\Environment;
  12. class ImportAlertListener
  13. {
  14.     private $_em;
  15.     private $counter;
  16.     public function __construct(EntityManager $em, private readonly TranslatableMailer $mailer, private readonly Environment $templating, private readonly LoggerInterface $logger, private $from)
  17.     {
  18.         $this->_em $em;
  19.         $this->counter['overall'] = 0;
  20.     }
  21.     public function onIgnoredData(ImportEvent $event): void
  22.     {
  23.         $data $event->getObject();
  24.         if (isset($data->status) && 'D' === $data->status) {
  25.             if (null !== $vehicle $this->_em->getRepository(Vehicle::class)->findOneByVehicleId($data->id)) {
  26.                 if (Vehicle::SALING_STATE_SUPPRIME === $vehicle->getSalingState()) {
  27.                     $this->counter['current'] = 0;
  28.                     foreach ($vehicle->getAlerts() as $alert) {
  29.                         $this->sendDeleted($alert->getUser(), $vehicle);
  30.                         $this->_em->remove($alert);
  31.                         ++$this->counter['current'];
  32.                     }
  33.                     foreach ($vehicle->getSelections() as $selection) {
  34.                         $this->_em->remove($selection);
  35.                     }
  36.                     $this->_em->flush();
  37.                     $this->logger->info($this->counter['current'].' deletion alert(s) sent for this Vehicle.');
  38.                 }
  39.             }
  40.         }
  41.     }
  42.     public function onPersistedData(ImportEvent $event): void
  43.     {
  44.         $entity $event->getObject();
  45.         if ($entity instanceof Vehicle) {
  46.             $alerts $entity->getAlerts();
  47.             $this->counter['current'] = 0;
  48.             foreach ($alerts as $alert) {
  49.                 if ($entity->isStartingPriceAvailable() && $alert->getLastPrice() !== $entity->getStartingPrice()) {
  50.                     $this->send($alert);
  51.                     $alert->setLastPrice($entity->getStartingPrice());
  52.                     $this->_em->flush($alert);
  53.                     ++$this->counter['current'];
  54.                     ++$this->counter['overall'];
  55.                 }
  56.             }
  57.             $this->logger->info($this->counter['current'].' alert(s) sent for this Vehicle.');
  58.         }
  59.     }
  60.     public function onEnd(): void
  61.     {
  62.         $this->logger->info($this->counter['overall'].' alert(s) sent overall.');
  63.     }
  64.     private function send(VehicleAlert $alert): void
  65.     {
  66.         try {
  67.             $this->mailer->send(
  68.                 $alert->getUser(),
  69.                 '/Mail/alert_vehicle_price.html.twig',
  70.                 ['alert' => $alert]
  71.             );
  72.         } catch (TransportException $e) {
  73.             $this->logger->warning(sprintf('Couldn\'t sent email to %s. Error message : %s'$alert->getUser()->getEmail(), $e->getMessage()));
  74.         }
  75.     }
  76.     private function sendDeleted(User $userVehicle $vehicle): void
  77.     {
  78.         try {
  79.             $this->mailer->send(
  80.                 $user,
  81.                 '/Mail/alert_vehicle_deleted.html.twig',
  82.                 ['vehicle' => $vehicle]
  83.             );
  84.         } catch (TransportException $e) {
  85.             $this->logger->warning(sprintf('Couldn\'t sent email to %s. Error message : %s'$user->getEmail(), $e->getMessage()));
  86.         }
  87.     }
  88. }