<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Event\ProcedureInvoicePaidEvent;
use App\Events;
use App\Service\EmailParametersGenerator;
use App\Service\Sendinblue\SMTPApi;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProcedureInvoicePaidSubscriber implements EventSubscriberInterface
{
public const EMAIL_TEMPLATE_ID = '25'; // TODO: change this to the correct paid template - NOT READY YET
/**
* @var EmailParametersGenerator
*/
protected $parametersGenerator;
/**
* @var SMTPApi
*/
protected $smtp;
public function __construct(EmailParametersGenerator $parametersGenerator, SMTPApi $smtp)
{
$this->parametersGenerator = $parametersGenerator;
$this->smtp = $smtp;
}
/**
* @param ProcedureInvoicePaidEvent $event
*/
public function onInvoicePaid(ProcedureInvoicePaidEvent $event): void
{
$procedure = $event->getProcedure();
$invoice = $procedure->getInvoice();
$parameters = $this->parametersGenerator->generateForProcedureStep($procedure->getDispatchedStep(), $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,
[]
);
}
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
Events::PROCEDURE_INVOICE_PAID => 'onInvoicePaid'
];
}
}