src/Queue/ProcedureQueueSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the CosaVostra, TrackPay package.
  4.  *
  5.  * (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Queue;
  12. use App\Connector\DriverRegistry;
  13. use App\Entity\Media;
  14. use App\Entity\Procedure;
  15. use App\Service\Sendinblue\SMTPApi;
  16. use App\Service\UrlGeneratorService;
  17. use DateTime;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Exception;
  20. use JMS\Serializer\Serializer;
  21. use JMS\Serializer\SerializerInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\File\File;
  24. use function Sentry\captureException;
  25. class ProcedureQueueSubscriber implements EventSubscriberInterface
  26. {
  27.     public const EMAIL_TEMPLATE_ID '26';
  28.     /**
  29.      * @var SMTPApi
  30.      */
  31.     protected $smtp;
  32.     /**
  33.      * @var SerializerInterface|Serializer
  34.      */
  35.     protected $serializer;
  36.     /**
  37.      * @var DriverRegistry
  38.      */
  39.     protected $driverRegistry;
  40.     /**
  41.      * @var UrlGeneratorService
  42.      */
  43.     protected $urlGenerator;
  44.     /**
  45.      * @var EntityManagerInterface
  46.      */
  47.     protected $entityManager;
  48.     public function __construct(
  49.         SMTPApi $smtp,
  50.         SerializerInterface $serializer,
  51.         DriverRegistry $driverRegistry,
  52.         UrlGeneratorService $urlGenerator,
  53.         EntityManagerInterface $entityManager
  54.     )
  55.     {
  56.         $this->smtp           $smtp;
  57.         $this->serializer     $serializer;
  58.         $this->driverRegistry $driverRegistry;
  59.         $this->urlGenerator   $urlGenerator;
  60.         $this->entityManager  $entityManager;
  61.     }
  62.     /**
  63.      * @inheritDoc
  64.      */
  65.     public static function getSubscribedEvents()
  66.     {
  67.         return [
  68.             ProcedureQueueEvent::PROCEDURE_QUEUE_CLOSE => 'onClose'
  69.         ];
  70.     }
  71.     public function onClose(ProcedureQueueEvent $event): void
  72.     {
  73.         // SEND EMAIL NOTIFICATION TO DEBTOR TO NOTIFY HIM ABOUT THE CLOSURE.
  74.         $procedure $event->getProcedure();
  75.         $invoice   $procedure->getInvoice();
  76.         if ($procedure->isClosed() &&
  77.             null !== $procedure->getConnector() &&
  78.             $procedure->getCloseReason() === Procedure::CLOSE_REASON_PAID) {
  79.             // Here we need to update the remote invoice status.
  80.             $loader $this->driverRegistry->connect($procedure->getConnector());
  81.             try {
  82.                 $loader->createPayment($invoice);
  83.                 sleep(2); // Wait until the third-part system update the invoice file.
  84.                 // Update invoice file.
  85.                 $invoiceFile $loader->getInvoiceFile($invoice);
  86.                 if (is_string($invoiceFile)) {
  87.                     $invoice->setFileUrl($invoiceFile);
  88.                 }
  89.                 if ($invoiceFile instanceof File) {
  90.                     // Set the pdf file
  91.                     if (null !== $invoiceMedia $invoice->getInvoiceFile()) {
  92.                         $invoiceMedia->setFile($invoiceFile);
  93.                     } else {
  94.                         $invoice->setInvoiceFile(
  95.                             (new Media())->setFile($invoiceFile)
  96.                         );
  97.                     }
  98.                 }
  99.                 $this->entityManager->flush();
  100.             } catch (Exception $exception) {
  101.                 captureException($exception);
  102.                 $event->setFailed(true$exception->getMessage());
  103.                 return;
  104.             }
  105.         }
  106.         if (!$procedure->hasStepsSentToCustomer()) {
  107.             return;
  108.         }
  109.         $logo null;
  110.         if (null !== $logoMedia $invoice->getAccount()->getLogo()) {
  111.             $logo $this->urlGenerator->getMediaFileUrl($logoMedia);
  112.         }
  113.         $invoiceSerialized $this->serializer->toArray($invoice);
  114.         if (null !== $invoice->getInvoiceFile()) {
  115.             $invoiceSerialized['full_file_url'] = $this->urlGenerator->generate('app_page_procedure_invoice', [
  116.                 'procedure' => $procedure->getId()
  117.             ]);
  118.         }
  119.         $parameters = [
  120.             'invoice' => $invoiceSerialized,
  121.             'reason'  => $procedure->getCloseReason(),
  122.             'info'    => [
  123.                 'now' => (new DateTime())->format(DATE_ATOM)
  124.             ],
  125.             'logo'    => $logo
  126.         ];
  127.         $contacts $invoice->getActiveCustomerContacts();
  128.         if ($contacts->isEmpty()) {
  129.             $this->smtp->send(
  130.                 $invoice->getDebtorName(),
  131.                 $invoice->getDebtorEmail(),
  132.                 self::EMAIL_TEMPLATE_ID,
  133.                 $parameters,
  134.                 []
  135.             );
  136.         } else {
  137.             $this->smtp->sendToContacts(
  138.                 $contacts,
  139.                 self::EMAIL_TEMPLATE_ID,
  140.                 $parameters,
  141.                 []
  142.             );
  143.         }
  144.     }
  145. }