<?php
namespace App\Service\App\Merchant\Integrations\Zapier\Listener;
use App\Background\Data\ZapierTriggerInvoiceData;
use App\Core\BackgroundWork\Contract\IClient;
use App\Core\MerchantSelector\Contract\IMerchantSelector;
use App\Service\App\Merchant\CustomFeature\Implementation\InvoiceCustomFeature;
use App\Service\App\Merchant\CustomFeature\Implementation\ZapierCustomFeature;
use App\Service\App\Merchant\Invoice\Event\FinalizeInvoiceEvent;
use App\Types\ZapierTriggerTypes;
class FinalizeInvoiceListener
{
/**
* Background job client
*
* @var IClient
*/
private IClient $backgroundClient;
/**
* Merchant selector
*
* @var IMerchantSelector
*/
private IMerchantSelector $merchantSelector;
/**
* @var ZapierCustomFeature
*/
private ZapierCustomFeature $zapierCustomFeature;
/**
* @var InvoiceCustomFeature
*/
private InvoiceCustomFeature $invoiceCustomFeature;
/**
* @param IClient $backgroundClient
* @param IMerchantSelector $merchantSelector
* @param ZapierCustomFeature $zapierCustomFeature
* @param InvoiceCustomFeature $invoiceCustomFeature
*/
public function __construct(
IClient $backgroundClient, IMerchantSelector $merchantSelector,
ZapierCustomFeature $zapierCustomFeature, InvoiceCustomFeature $invoiceCustomFeature
)
{
$this->backgroundClient = $backgroundClient;
$this->merchantSelector = $merchantSelector;
$this->zapierCustomFeature = $zapierCustomFeature;
$this->invoiceCustomFeature = $invoiceCustomFeature;
}
/**
* @param FinalizeInvoiceEvent $event
* @return void
*/
public function onAction(FinalizeInvoiceEvent $event): void
{
if (!$this->zapierCustomFeature->isEnabled() || !$this->invoiceCustomFeature->isEnabled()) {
return;
}
$this->backgroundClient->runJob(
new ZapierTriggerInvoiceData(
$event->getInvoiceId(), $this->merchantSelector->getMerchant(),
ZapierTriggerTypes::TYPE_INVOICE_FINALIZED
)
);
}
}