src/Subscriber/Billing/PayoutSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Billing;
  3. use App\Entity\Billing;
  4. use App\Event\Payment\PayoutPublishedEvent;
  5. use App\Lib\System\Job\PayoutEmailJob;
  6. use App\Service\Payment\BillingService;
  7. use App\Service\System\JobService;
  8. use App\Service\User\AccountService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class PayoutSubscriber implements EventSubscriberInterface
  11. {
  12.     protected BillingService $service;
  13.     protected JobService $jobService;
  14.     protected AccountService $accountService;
  15.     /**
  16.      * PayoutSubscriber constructor.
  17.      */
  18.     public function __construct(BillingService $billingServiceJobService $jobServiceAccountService $accountService)
  19.     {
  20.         $this->service $billingService;
  21.         $this->jobService $jobService;
  22.         $this->accountService $accountService;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             PayoutPublishedEvent::class => [
  28.                 ['onPayoutPublishedUpdateBills'0],
  29.                 ['onPayoutPublishedSendNotifications'1],
  30.             ],
  31.         ];
  32.     }
  33.     /**
  34.      * @throws \Doctrine\DBAL\Exception
  35.      */
  36.     public function onPayoutPublishedUpdateBills(PayoutPublishedEvent $event)
  37.     {
  38.         $published $event->isPublished();
  39.         $payout $event->getPayout();
  40.         $this->service->markBillsAsPublishedForPayout($payout$published);
  41.     }
  42.     /**
  43.      * @throws \Doctrine\ORM\ORMException
  44.      * @throws \Doctrine\ORM\OptimisticLockException
  45.      */
  46.     public function onPayoutPublishedSendNotifications(PayoutPublishedEvent $event)
  47.     {
  48.         if (!$event->sendNotifications()) {
  49.             return;
  50.         }
  51.         $payout $event->getPayout();
  52.         $bills $this->service->getPublishedBillsForPayout($payout);
  53.         foreach ($bills as $bill) {
  54.             /**
  55.              * @var $bill Billing
  56.              */
  57.             $job = new PayoutEmailJob($bill->getId(), $payout->getId());
  58.             if ($bill->getAmateur()) {
  59.                 $account $bill->getAmateur()->getAccount();
  60.                 $job->setAutologinParam($this->accountService->getAutologinParameter($account));
  61.                 $job->setTemplate('FrivolCoreBundle:Email:payout-amateur.mail.twig');
  62.             } elseif ($bill->getWebmaster()) {
  63.                 $account $bill->getWebmaster()->getAccount();
  64.                 $job->setAutologinParam($this->accountService->getAutologinParameter($account));
  65.                 $job->setTemplate('FrivolCoreBundle:Email:payout-webmaster.mail.twig');
  66.             }
  67.             $this->jobService->createSaveAndSchedule($job''nullPayoutEmailJob::queue);
  68.         }
  69.     }
  70. }