src/Subscriber/Balance/ChargebackProvisionSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Balance;
  3. use App\Dictionary\BalanceType;
  4. use App\Dictionary\PaymentType;
  5. use App\Event\Payment\PaymentCreatedEvent;
  6. use App\Service\Amateur\BalanceAmateurService;
  7. use App\Service\Payment\PaymentService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ChargebackProvisionSubscriber implements EventSubscriberInterface
  10. {
  11.     protected BalanceAmateurService $balanceAmateurService;
  12.     protected PaymentService $paymentService;
  13.     /**
  14.      * ChargebackProvisionSubscriber constructor.
  15.      */
  16.     public function __construct(BalanceAmateurService $balanceAmateurServicePaymentService $paymentService)
  17.     {
  18.         $this->balanceAmateurService $balanceAmateurService;
  19.         $this->paymentService $paymentService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             PaymentCreatedEvent::class => [
  25.                 ['revokeAmateurProvision'1],
  26.             ],
  27.         ];
  28.     }
  29.     protected function getService(): BalanceAmateurService
  30.     {
  31.         return $this->balanceAmateurService;
  32.     }
  33.     /**
  34.      * @throws \App\Exception\Commission\NotAnAmateurException
  35.      * @throws \Doctrine\ORM\ORMException
  36.      * @throws \Doctrine\ORM\OptimisticLockException
  37.      */
  38.     public function revokeAmateurProvision(PaymentCreatedEvent $event)
  39.     {
  40.         $chargeback $event->getPayment();
  41.         if (PaymentType::CHARGEBACK !== $chargeback->getType()) {
  42.             return;
  43.         }
  44.         // search for the initial booking
  45.         $booking $this->paymentService->getOriginalPaymentOfType($chargebackPaymentType::BOOKING);
  46.         if (!$booking) {
  47.             return;
  48.         }
  49.         $service $this->getService();
  50.         // incomeBalances are the amateur provisions for the original booking
  51.         $incomeBalances $service->getAmateurBalancesOfTypeByPayment($bookingBalanceType::INCOME);
  52.         foreach ($incomeBalances as $balanceAmateur) {
  53.             $member $balanceAmateur->getMember();
  54.             $balance $service->createProvisionByAmateur($memberBalanceType::CHARGEBACK);
  55.             // negate the previous provision
  56.             $balance->setAmount($balanceAmateur->getAmount() * -1);
  57.             $balance->setBalanceDetail($balanceAmateur->getBalanceDetail());
  58.             $balance->setPayment($chargeback); // the chargeback from the event
  59.             $service->storeBalanceAmateur($balance);
  60.         }
  61.     }
  62. }