<?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\EventSubscriber;
use App\Event\ProcedureResumeRemindEvent;
use App\Service\EmailParametersGenerator;
use App\Service\Sendinblue\SMTPApi;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProcedureResumeRemindSubscriber implements EventSubscriberInterface
{
public const EMAIL_TEMPLATE_ID = '21'; // TODO: change this to the correct snooze template - NOT READY YET
/**
* @var SMTPApi
*/
protected $smtp;
/**
* @var EmailParametersGenerator
*/
protected $parametersGenerator;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
public function __construct(SMTPApi $smtp, EmailParametersGenerator $parametersGenerator, EntityManagerInterface $entityManager)
{
$this->smtp = $smtp;
$this->parametersGenerator = $parametersGenerator;
$this->entityManager = $entityManager;
}
/**
* @param ProcedureResumeRemindEvent $event
*/
public function onResumeRemind(ProcedureResumeRemindEvent $event): void
{
$procedure = $event->getProcedure();
$dispatchedStep = $procedure->getDispatchedStep();
$invoice = $procedure->getInvoice();
if (null === $dispatchedStep) {
// Do nothing.
return;
}
$parameters = $this->parametersGenerator->generateForProcedureStep($dispatchedStep, $invoice->getAccountLocale());
$contacts = $invoice->getActiveAccountContacts();
if ($contacts->isEmpty()) {
$this->smtp->send(
$invoice->getCreditorName(),
$invoice->getCreditorEmail(),
self::EMAIL_TEMPLATE_ID,
$parameters,
[]
);
} else {
$this->smtp->sendToContacts(
$contacts,
self::EMAIL_TEMPLATE_ID,
$parameters,
[]
);
}
$procedure->setResumeReminded(true);
$this->entityManager->flush();
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
ProcedureResumeRemindEvent::class => 'onResumeRemind'
];
}
}