<?php
namespace App\Subscriber\Billing;
use App\Entity\Billing;
use App\Event\Payment\PayoutPublishedEvent;
use App\Lib\System\Job\PayoutEmailJob;
use App\Service\Payment\BillingService;
use App\Service\System\JobService;
use App\Service\User\AccountService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PayoutSubscriber implements EventSubscriberInterface
{
protected BillingService $service;
protected JobService $jobService;
protected AccountService $accountService;
/**
* PayoutSubscriber constructor.
*/
public function __construct(BillingService $billingService, JobService $jobService, AccountService $accountService)
{
$this->service = $billingService;
$this->jobService = $jobService;
$this->accountService = $accountService;
}
public static function getSubscribedEvents(): array
{
return [
PayoutPublishedEvent::class => [
['onPayoutPublishedUpdateBills', 0],
['onPayoutPublishedSendNotifications', 1],
],
];
}
/**
* @throws \Doctrine\DBAL\Exception
*/
public function onPayoutPublishedUpdateBills(PayoutPublishedEvent $event)
{
$published = $event->isPublished();
$payout = $event->getPayout();
$this->service->markBillsAsPublishedForPayout($payout, $published);
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onPayoutPublishedSendNotifications(PayoutPublishedEvent $event)
{
if (!$event->sendNotifications()) {
return;
}
$payout = $event->getPayout();
$bills = $this->service->getPublishedBillsForPayout($payout);
foreach ($bills as $bill) {
/**
* @var $bill Billing
*/
$job = new PayoutEmailJob($bill->getId(), $payout->getId());
if ($bill->getAmateur()) {
$account = $bill->getAmateur()->getAccount();
$job->setAutologinParam($this->accountService->getAutologinParameter($account));
$job->setTemplate('FrivolCoreBundle:Email:payout-amateur.mail.twig');
} elseif ($bill->getWebmaster()) {
$account = $bill->getWebmaster()->getAccount();
$job->setAutologinParam($this->accountService->getAutologinParameter($account));
$job->setTemplate('FrivolCoreBundle:Email:payout-webmaster.mail.twig');
}
$this->jobService->createSaveAndSchedule($job, '', null, PayoutEmailJob::queue);
}
}
}