src/Service/App/Merchant/Integrations/TeamBlue/Listener/FinalizeInvoiceListener.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Service\App\Merchant\Integrations\TeamBlue\Listener;
  3. use App\Background\Data\TeamBlueSyncInvoiceData;
  4. use App\Core\BackgroundWork\Contract\IClient;
  5. use App\Core\MerchantSelector\Contract\IMerchantSelector;
  6. use App\Service\App\Merchant\CustomFeature\Implementation\TeamBlueCustomFeature;
  7. use App\Service\App\Merchant\Invoice\Event\FinalizeInvoiceEvent;
  8. class FinalizeInvoiceListener
  9. {
  10.     /**
  11.      * Background job client
  12.      *
  13.      * @var IClient
  14.      */
  15.     private IClient $backgroundClient;
  16.     /**
  17.      * Merchant selector
  18.      *
  19.      * @var IMerchantSelector
  20.      */
  21.     private IMerchantSelector $merchantSelector;
  22.     /**
  23.      * Team blue custom feature instance
  24.      *
  25.      * @var TeamBlueCustomFeature
  26.      */
  27.     private TeamBlueCustomFeature $teamBlueCustomFeature;
  28.     /**
  29.      * Constructor
  30.      *
  31.      * @param TeamBlueCustomFeature $teamBlueCustomFeature
  32.      * @param IClient $backgroundClient
  33.      * @param IMerchantSelector $merchantSelector
  34.      */
  35.     public function __construct(
  36.         TeamBlueCustomFeature $teamBlueCustomFeature,
  37.         IClient $backgroundClient,
  38.         IMerchantSelector $merchantSelector
  39.     )
  40.     {
  41.         $this->teamBlueCustomFeature $teamBlueCustomFeature;
  42.         $this->backgroundClient $backgroundClient;
  43.         $this->merchantSelector $merchantSelector;
  44.     }
  45.     /**
  46.      * Listen on invoice finalized event
  47.      *
  48.      * @param FinalizeInvoiceEvent $event
  49.      * @return void
  50.      */
  51.     public function onAction(FinalizeInvoiceEvent $event): void
  52.     {
  53.         // skip if custom feature is not enabled or invoice is paid.
  54.         // if invoice is paid, it will be synced by PaidInvoiceListener
  55.         if (!$this->teamBlueCustomFeature->isEnabled() || $event->isPaid()) {
  56.             return;
  57.         }
  58.         $this->backgroundClient->runJob(
  59.             new TeamBlueSyncInvoiceData($event->getInvoiceId(), $this->merchantSelector->getMerchant())
  60.         );
  61.     }
  62. }