src/EventSubscriber/Payment/PaymentPromotionCountSubscriber.php line 30

  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\Payment\Payment;
  13. use App\Entity\Promotion\PromotionCode;
  14. use Sylius\Component\Resource\Repository\RepositoryInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Workflow\Event\Event;
  17. use Webmozart\Assert\Assert;
  18. class PaymentPromotionCountSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private RepositoryInterface $promotionCodeRepository,
  22.     ) {
  23.     }
  24.     public function onEnter(Event $event)
  25.     {
  26.         /** @var Payment $payment */
  27.         $payment $event->getSubject();
  28.         Assert::isInstanceOf($paymentPayment::class);
  29.         /** @var PromotionCode|null $promotionCode */
  30.         $promotionCode $payment->getPromotionCode();
  31.         if (null !== $promotionCode) {
  32.             $promotionCode->setUsed($promotionCode->getUsed() + 1);
  33.             $promotionCode->setUsedAt(new \DateTime());
  34.             if (null !== $payment->getCustomer()) {
  35.                 $promotionCode->setUserEmail($payment->getCustomer()->getEmail());
  36.             }
  37.             $this->promotionCodeRepository->add($promotionCode);
  38.         }
  39.     }
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             'workflow.payment.enter.completed' => 'onEnter',
  44.         ];
  45.     }
  46. }