<?php
/**
* This file is part of the CosaVostra, TrackPay package.
*
* (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Queue;
use App\Connector\DriverRegistry;
use App\Entity\Media;
use App\Entity\Procedure;
use App\Service\Sendinblue\SMTPApi;
use App\Service\UrlGeneratorService;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\File\File;
use function Sentry\captureException;
class ProcedureQueueSubscriber implements EventSubscriberInterface
{
public const EMAIL_TEMPLATE_ID = '26';
/**
* @var SMTPApi
*/
protected $smtp;
/**
* @var SerializerInterface|Serializer
*/
protected $serializer;
/**
* @var DriverRegistry
*/
protected $driverRegistry;
/**
* @var UrlGeneratorService
*/
protected $urlGenerator;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
public function __construct(
SMTPApi $smtp,
SerializerInterface $serializer,
DriverRegistry $driverRegistry,
UrlGeneratorService $urlGenerator,
EntityManagerInterface $entityManager
)
{
$this->smtp = $smtp;
$this->serializer = $serializer;
$this->driverRegistry = $driverRegistry;
$this->urlGenerator = $urlGenerator;
$this->entityManager = $entityManager;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
ProcedureQueueEvent::PROCEDURE_QUEUE_CLOSE => 'onClose'
];
}
public function onClose(ProcedureQueueEvent $event): void
{
// SEND EMAIL NOTIFICATION TO DEBTOR TO NOTIFY HIM ABOUT THE CLOSURE.
$procedure = $event->getProcedure();
$invoice = $procedure->getInvoice();
if ($procedure->isClosed() &&
null !== $procedure->getConnector() &&
$procedure->getCloseReason() === Procedure::CLOSE_REASON_PAID) {
// Here we need to update the remote invoice status.
$loader = $this->driverRegistry->connect($procedure->getConnector());
try {
$loader->createPayment($invoice);
sleep(2); // Wait until the third-part system update the invoice file.
// Update invoice file.
$invoiceFile = $loader->getInvoiceFile($invoice);
if (is_string($invoiceFile)) {
$invoice->setFileUrl($invoiceFile);
}
if ($invoiceFile instanceof File) {
// Set the pdf file
if (null !== $invoiceMedia = $invoice->getInvoiceFile()) {
$invoiceMedia->setFile($invoiceFile);
} else {
$invoice->setInvoiceFile(
(new Media())->setFile($invoiceFile)
);
}
}
$this->entityManager->flush();
} catch (Exception $exception) {
captureException($exception);
$event->setFailed(true, $exception->getMessage());
return;
}
}
if (!$procedure->hasStepsSentToCustomer()) {
return;
}
$logo = null;
if (null !== $logoMedia = $invoice->getAccount()->getLogo()) {
$logo = $this->urlGenerator->getMediaFileUrl($logoMedia);
}
$invoiceSerialized = $this->serializer->toArray($invoice);
if (null !== $invoice->getInvoiceFile()) {
$invoiceSerialized['full_file_url'] = $this->urlGenerator->generate('app_page_procedure_invoice', [
'procedure' => $procedure->getId()
]);
}
$parameters = [
'invoice' => $invoiceSerialized,
'reason' => $procedure->getCloseReason(),
'info' => [
'now' => (new DateTime())->format(DATE_ATOM)
],
'logo' => $logo
];
$contacts = $invoice->getActiveCustomerContacts();
if ($contacts->isEmpty()) {
$this->smtp->send(
$invoice->getDebtorName(),
$invoice->getDebtorEmail(),
self::EMAIL_TEMPLATE_ID,
$parameters,
[]
);
} else {
$this->smtp->sendToContacts(
$contacts,
self::EMAIL_TEMPLATE_ID,
$parameters,
[]
);
}
}
}