<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Service\InvoiceGenerator;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class InvoiceSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly InvoiceGenerator $invoiceGenerator,
private readonly LoggerInterface $logger,
) {
}
public static function getSubscribedEvents(): array
{
return [
'sylius.payment.post_complete' => 'onPaymentCompleted',
];
}
public function onPaymentCompleted(mixed $event): void
{
if (!is_object($event) || !method_exists($event, 'getSubject')) {
return;
}
$payment = $event->getSubject();
if (!is_object($payment) || !method_exists($payment, 'getOrder')) {
return;
}
$order = $payment->getOrder();
if (null === $order) {
return;
}
try {
$this->invoiceGenerator->ensureGenerated($order);
} catch (\Throwable $exception) {
$this->logger->error('Invoice generation subscriber failed.', [
'exception' => $exception::class,
'message' => $exception->getMessage(),
]);
}
}
}