<?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\EmptyInvoiceContactEvent;
use App\Service\EmailParametersGenerator;
use App\Service\Sendinblue\SMTPApi;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EmptyInvoiceContactSubscriber implements EventSubscriberInterface
{
public const EMAIL_TEMPLATE_ID = '29';
/**
* @var EmailParametersGenerator
*/
protected $emailParametersGenerator;
/**
* @var SMTPApi
*/
protected $smtp;
public function __construct(EmailParametersGenerator $emailParametersGenerator, SMTPApi $smtp)
{
$this->emailParametersGenerator = $emailParametersGenerator;
$this->smtp = $smtp;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
EmptyInvoiceContactEvent::class => 'onEmptyInvoiceContact'
];
}
public function onEmptyInvoiceContact(EmptyInvoiceContactEvent $event): void
{
$invoice = $event->getInvoice();
$procedure = $invoice->getProcedure();
$emailParams = $this->emailParametersGenerator->generateForProcedureStep(
$procedure->getDispatchedStep() ?? $procedure->getPendingStep(),
$invoice->getAccountLocale(),
['empty_customer_contacts' => true, 'procedure' => $procedure->getId()]
);
// Send email to the user's email.
$user = $invoice->getUser();
// Send notification email for user to alert him
// that he needs to add at least one customer contact
$this->smtp->send(
$user->getFullName(),
$user->getUsername(),
static::EMAIL_TEMPLATE_ID,
$emailParams,
[]
);
}
}