src/Service/App/Merchant/FecReport/Listener/InvoiceListener.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\Service\App\Merchant\FecReport\Listener;
  3. use App\Background\Data\AddFecEntryData;
  4. use App\Core\BackgroundWork\Contract\IClient;
  5. use App\Core\MerchantSelector\Contract\IMerchantSelector;
  6. use App\Service\App\Merchant\CustomFeature\Implementation\FecReportCustomFeature;
  7. use App\Service\App\Merchant\Invoice\Event\FinalizeInvoiceEvent;
  8. use App\Service\App\Merchant\Invoice\Event\PaidInvoiceEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class InvoiceListener implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * Client
  14.      *
  15.      * @var IClient
  16.      */
  17.     private IClient $client;
  18.     /**
  19.      * Merchant selector
  20.      *
  21.      * @var IMerchantSelector
  22.      */
  23.     private IMerchantSelector $merchantSelector;
  24.     /**
  25.      * FEC report custom feature
  26.      *
  27.      * @var FecReportCustomFeature
  28.      */
  29.     private FecReportCustomFeature $fecReportCustomFeature;
  30.     /**
  31.      * Constructor
  32.      *
  33.      * @param IClient $client
  34.      * @param IMerchantSelector $merchantSelector
  35.      * @param FecReportCustomFeature $fecReportCustomFeature
  36.      */
  37.     public function __construct(
  38.         IClient $client,
  39.         IMerchantSelector $merchantSelector,
  40.         FecReportCustomFeature $fecReportCustomFeature
  41.     )
  42.     {
  43.         $this->client $client;
  44.         $this->merchantSelector $merchantSelector;
  45.         $this->fecReportCustomFeature $fecReportCustomFeature;
  46.     }
  47.     /**
  48.      * Returns an array of event names this subscriber wants to listen to.
  49.      *
  50.      * The array keys are event names and the value can be:
  51.      *
  52.      *  * The method name to call (priority defaults to 0)
  53.      *  * An array composed of the method name to call and the priority
  54.      *  * An array of arrays composed of the method names to call and respective
  55.      *    priorities, or 0 if unset
  56.      *
  57.      * For instance:
  58.      *
  59.      *  * ['eventName' => 'methodName']
  60.      *  * ['eventName' => ['methodName', $priority]]
  61.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  62.      *
  63.      * The code must not depend on runtime state as it will only be called at compile time.
  64.      * All logic depending on runtime state must be put into the individual methods handling the events.
  65.      *
  66.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  67.      */
  68.     public static function getSubscribedEvents()
  69.     {
  70.         return [
  71.             FinalizeInvoiceEvent::NAME => 'onFinalizeInvoice',
  72.             PaidInvoiceEvent::NAME => 'onPaidInvoice',
  73.         ];
  74.     }
  75.     /**
  76.      * On finalize invoice
  77.      *
  78.      * @param FinalizeInvoiceEvent $event
  79.      * @return void
  80.      */
  81.     public function onFinalizeInvoice(FinalizeInvoiceEvent $event): void
  82.     {
  83.         if (!$this->fecReportCustomFeature->isEnabled()) {
  84.             return;
  85.         }
  86.         // if invoice is paid just skip it, as we will run task in onPaidInvoice
  87.         if ($event->isPaid()) {
  88.             return;
  89.         }
  90.         $this->client->runJob(new AddFecEntryData($this->merchantSelector->getMerchant(), $event->getInvoiceId()));
  91.     }
  92.     /**
  93.      * On paid invoice event
  94.      *
  95.      * @param PaidInvoiceEvent $event
  96.      * @return void
  97.      */
  98.     public function onPaidInvoice(PaidInvoiceEvent $event): void
  99.     {
  100.         if (!$this->fecReportCustomFeature->isEnabled()) {
  101.             return;
  102.         }
  103.         $this->client->runJob(new AddFecEntryData($this->merchantSelector->getMerchant(), $event->getInvoiceId()));
  104.     }
  105. }