src/EventSubscriber/Payment/PaymentAddCreditsSubscriber.php line 32

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber\Payment;
  12. use App\Entity\Customer\Customer;
  13. use App\Entity\Payment\Payment;
  14. use App\Entity\Promotion\PromotionCode;
  15. use App\Repository\CustomerRepository;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  18. use Symfony\Component\Workflow\Event\Event;
  19. use Webmozart\Assert\Assert;
  20. class PaymentAddCreditsSubscriber implements EventSubscriberInterface
  21. {
  22.     public function __construct(
  23.         private CustomerRepository $customerRepository,
  24.     ) {
  25.     }
  26.     public function onEnter(Event $event)
  27.     {
  28.         /** @var Payment $payment */
  29.         $payment $event->getSubject();
  30.         Assert::isInstanceOf($paymentPayment::class);
  31.         // Only add credits for credit pack purchases; order_direct/order_complement do not add credits
  32.         if ($payment->isOrderPayment()) {
  33.             return;
  34.         }
  35.         /** @var Customer $customer */
  36.         $customer $payment->getCustomer();
  37.         Assert::notNull($customer'Can not add credits without customer');
  38.         /** @var PromotionCode|null $promotionCode */
  39.         $promotionCode $payment->getPromotionCode();
  40.         if (null !== $promotionCode?->getUniquePromotion() && !== $promotionCode->getUsed()) {
  41.             throw new BadRequestHttpException('Promotion code already used');
  42.         }
  43.         $customer->setCredits($customer->getCredits() + $payment->getCredits());
  44.         $this->customerRepository->add($customer);
  45.     }
  46.     public static function getSubscribedEvents()
  47.     {
  48.         return [
  49.             'workflow.payment.enter.completed' => 'onEnter',
  50.         ];
  51.     }
  52. }