src/EventSubscriber/InvoiceSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Service\InvoiceGenerator;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. final class InvoiceSubscriber implements EventSubscriberInterface
  8. {
  9. public function __construct(
  10. private readonly InvoiceGenerator $invoiceGenerator,
  11. private readonly LoggerInterface $logger,
  12. ) {
  13. }
  14. public static function getSubscribedEvents(): array
  15. {
  16. return [
  17. 'sylius.payment.post_complete' => 'onPaymentCompleted',
  18. ];
  19. }
  20. public function onPaymentCompleted(mixed $event): void
  21. {
  22. if (!is_object($event) || !method_exists($event, 'getSubject')) {
  23. return;
  24. }
  25. $payment = $event->getSubject();
  26. if (!is_object($payment) || !method_exists($payment, 'getOrder')) {
  27. return;
  28. }
  29. $order = $payment->getOrder();
  30. if (null === $order) {
  31. return;
  32. }
  33. try {
  34. $this->invoiceGenerator->ensureGenerated($order);
  35. } catch (\Throwable $exception) {
  36. $this->logger->error('Invoice generation subscriber failed.', [
  37. 'exception' => $exception::class,
  38. 'message' => $exception->getMessage(),
  39. ]);
  40. }
  41. }
  42. }