src/Invoice/EventSubscriber/SendPaymentNotificationSubscriber.php line 55

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2022 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2022 TECLA Consulting Group oü
  16.  */
  17. namespace App\Invoice\EventSubscriber;
  18. use App\Invoice\Event\InvoicePayment\CreatedEvent;
  19. use App\Invoice\Query\InvoicePaymentNotification\FindAllSuitableRecipientsQueryInterface;
  20. use App\Invoice\Util\InvoicePaymentNotificationMailerInterface;
  21. use JetBrains\PhpStorm\ArrayShape;
  22. use Psr\Log\LoggerInterface;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Throwable;
  25. final class SendPaymentNotificationSubscriber implements EventSubscriberInterface
  26. {
  27.     private FindAllSuitableRecipientsQueryInterface $findAllSuitableRecipientsQuery;
  28.     private InvoicePaymentNotificationMailerInterface $invoicePaymentNotificationMailer;
  29.     private LoggerInterface $logger;
  30.     public function __construct(
  31.         FindAllSuitableRecipientsQueryInterface $findAllSuitableRecipientsQuery,
  32.         InvoicePaymentNotificationMailerInterface $invoicePaymentNotificationMailer,
  33.         LoggerInterface $logger
  34.     ) {
  35.         $this->findAllSuitableRecipientsQuery $findAllSuitableRecipientsQuery;
  36.         $this->invoicePaymentNotificationMailer $invoicePaymentNotificationMailer;
  37.         $this->logger $logger;
  38.     }
  39.     #[ArrayShape([CreatedEvent::class => 'string'])]
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             CreatedEvent::class => 'onCreated',
  44.         ];
  45.     }
  46.     public function onCreated(CreatedEvent $event): void
  47.     {
  48.         $invoicePayment $event->getInvoicePayment();
  49.         $invoice $invoicePayment->getInvoice();
  50.         $supplier $invoice->getSupplier();
  51.         $customer $invoice->getCustomer();
  52.         $invoicePaymentNotifications $this->findAllSuitableRecipientsQuery->execute($supplier$customer);
  53.         foreach ($invoicePaymentNotifications as $invoicePaymentNotification) {
  54.             try {
  55.                 $this->invoicePaymentNotificationMailer->send(
  56.                     $invoicePaymentNotification->getSubscriber(),
  57.                     $invoicePayment
  58.                 );
  59.             } catch (Throwable $e) {
  60.                 $this->logger->error('Failed to send invoice payment notification', [
  61.                     'subscriber' => $invoicePaymentNotification->getSubscriber()->getId()->toRfc4122(),
  62.                     'invoicePayment' => $invoicePayment->getId()->toRfc4122(),
  63.                     'exception' => $e,
  64.                 ]);
  65.             }
  66.         }
  67.     }
  68. }