src/EventSubscriber/PromocodeSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\OrderMakedEvent;
  4. use App\Event\PromocodeEntered;
  5. use App\Service\Promocode\PromocodeService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /** @package App\EventSubscriber */
  9. class PromocodeSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(        
  12.         private PromocodeService $promocode,
  13.         private EntityManagerInterface $em,
  14.     )
  15.     {
  16.     
  17.     }    
  18.     public function onPromocodeEntered(PromocodeEntered $event): void
  19.     {        
  20.         $this->promocode->setPromocode($event->getCode(), $event->getDiscount());
  21.     }
  22.     public function onOrderMaked(OrderMakedEvent $event): void
  23.     {
  24.         $order $event->getOrder();
  25.         $order->setPromocode($this->promocode->getCode());
  26.         $this->em->flush();
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             PromocodeEntered::NAME => 'onPromocodeEntered',
  32.             OrderMakedEvent::NAME => 'onOrderMaked',
  33.         ];
  34.     }
  35. }