<?php
namespace App\EventSubscriber\ConsumptionExport;
use App\Event\ConsumptionExport\ConsumptionExportEvent;
use App\EventSubscriber\AbstractEmailSubscriber;
use App\Manager\Mailer;
use App\Manager\MailTypeInterface;
use App\Repository\App\UserRepository;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class ConsumptionExportSubscriber extends AbstractEmailSubscriber
{
public function __construct(
Mailer $mailer,
Environment $templating,
TranslatorInterface $translator,
private UserRepository $userRepository
)
{
parent::__construct($mailer, $templating, $translator);
}
public static function getSubscribedEvents(): array
{
return [
ConsumptionExportEvent::class => ['onConsumptionExport'],
];
}
public function onConsumptionExport(ConsumptionExportEvent $event): void
{
$consumptionExport = $event->getConsumptionExport();
$customerNumber = $consumptionExport->getCustomerNumber();
$user = $this->userRepository->findOneBy(
['customerNumber' => $customerNumber]
);
if (!$user) {
throw new \RuntimeException("Unable de retrieve user with customerNumber {$customerNumber}");
}
$this->mailer->sendMail(
[$user->getEmail()],
$this->transEmail('export_consumption.subject'),
$this->templating->render('email/consumption/export/exportCreation.twig', [
'customer' => $user,
'consumptionExport' => $consumptionExport,
]),
MailTypeInterface::CONSUMPTION_EXPORT
);
}
}