src/EventSubscriber/Promotion/PromotionCodeAddCreditPacksSubscriber.php line 38
<?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\Promotion;use ApiPlatform\Symfony\EventListener\EventPriorities;use App\Entity\Promotion\PromotionCode;use App\Modifier\Product\CreditPackModifier;use Sylius\Component\Resource\Repository\RepositoryInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;class PromotionCodeAddCreditPacksSubscriber implements EventSubscriberInterface{public function __construct(private RepositoryInterface $creditPackRepository,private CreditPackModifier $creditPackModifier,) {}public static function getSubscribedEvents(){return [ KernelEvents::VIEW => ['hydrateCreditPack', EventPriorities::PRE_SERIALIZE]];}public function hydrateCreditPack(ViewEvent $event): void{$promotionCode = $event->getControllerResult();$method = $event->getRequest()->getMethod();if (!$promotionCode instanceof PromotionCode || Request::METHOD_GET !== $method) {return;}$creditPacks = $this->creditPackRepository->findBy(['enabled' => true]);foreach ($creditPacks as $creditPack) {$this->creditPackModifier->addPromotion($creditPack, $promotionCode);$promotionCode->addCreditPack($creditPack);}}}