src/EventSubscriber/Payment/PaymentAddCreditsSubscriber.php line 32
<?php/** This file is part of the Pellipop project.** (c) Mobizel** For the full copyright and license information, please view the LICENSE* file that was distributed with this source code.*/declare(strict_types=1);namespace App\EventSubscriber\Payment;use App\Entity\Customer\Customer;use App\Entity\Payment\Payment;use App\Entity\Promotion\PromotionCode;use App\Repository\CustomerRepository;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;use Symfony\Component\Workflow\Event\Event;use Webmozart\Assert\Assert;class PaymentAddCreditsSubscriber implements EventSubscriberInterface{public function __construct(private CustomerRepository $customerRepository,) {}public function onEnter(Event $event){/** @var Payment $payment */$payment = $event->getSubject();Assert::isInstanceOf($payment, Payment::class);// Only add credits for credit pack purchases; order_direct/order_complement do not add creditsif ($payment->isOrderPayment()) {return;}/** @var Customer $customer */$customer = $payment->getCustomer();Assert::notNull($customer, 'Can not add credits without customer');/** @var PromotionCode|null $promotionCode */$promotionCode = $payment->getPromotionCode();if (null !== $promotionCode?->getUniquePromotion() && 0 !== $promotionCode->getUsed()) {throw new BadRequestHttpException('Promotion code already used');}$customer->setCredits($customer->getCredits() + $payment->getCredits());$this->customerRepository->add($customer);}public static function getSubscribedEvents(){return ['workflow.payment.enter.completed' => 'onEnter',];}}