<?phpnamespace App\EventSubscriber;use ApiPlatform\Core\EventListener\EventPriorities;use App\Entity\ItemType;use App\Entity\Order;use App\Entity\Showroom;use App\Entity\TaxRate;use App\Entity\User;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;final class AddInfoToOrderSubscriber implements EventSubscriberInterface{ /** * @var TokenStorageInterface */ private $tokenStorage; /** * @var EntityManagerInterface */ private $entityManager; public function __construct(TokenStorageInterface $tokenStorage, EntityManagerInterface $entityManager) { $this->tokenStorage = $tokenStorage; $this->entityManager = $entityManager; } public static function getSubscribedEvents() { return [ KernelEvents::VIEW => ['prewrite', EventPriorities::PRE_WRITE], ]; } public function prewrite(ViewEvent $event) { $order = $event->getControllerResult(); $method = $event->getRequest()->getMethod(); if (!$order instanceof Order || Request::METHOD_POST !== $method) { // Only handle Order entities (Event is called on any Api entity) return; } // maybe these extra null checks are not even needed $token = $this->tokenStorage->getToken(); if (!$token) { return; } $owner = $token->getUser(); if (!$owner instanceof User) { return; } // Attach the user to the not yet persisted Article $order->setOwner($owner); /* * SI prestaCarrier == 17 Retrait boutique Showroom Bordeaux - 12 Place Gambetta * Remplir l'adresse de livraison avec le showroom de bordeaux * */ if($order->getPrestaCarrier() == 17){ $order->setShippingAddressAddress("RETRAIT SHOWROOM BORDEAUX"); $order->setShippingAddressAddress2(""); $order->setShippingAddressCity("BORDEAUX"); $order->setShippingAddressState(" "); $order->setShippingAddressFirstname(" "); $order->setShippingAddressLastname(" "); $order->setShippingAddressCompany("LE JOAILLIER DU MARAIS BORDEAUX"); $order->setShippingAddressZipcode("33000"); } /* * SI prestaCarrier == 13 Retrait boutique Showroom Paris - 30 rue Réaumur * Remplir l'adresse de livraison avec le showroom de paris * */ if($order->getPrestaCarrier() == 13){ $order->setShippingAddressAddress("RETRAIT SHOWROOM PARIS"); $order->setShippingAddressAddress2(""); $order->setShippingAddressCity("PARIS"); $order->setShippingAddressState(" "); $order->setShippingAddressZipcode("75003"); $order->setShippingAddressFirstname(" "); $order->setShippingAddressLastname(" "); $order->setShippingAddressCompany("LE JOAILLIER DU MARAIS PARIS"); } $taxRate = $this->entityManager->getRepository(TaxRate::class)->findOneBy(["def" => 1]); $typesurmesure = $this->entityManager->getRepository(ItemType::class)->find(1); //RECUPERATION DES INFOS DU QUOTE ITEM foreach ($order->getItem() as $item) { $item->setTaxRate($taxRate); $item->setType($typesurmesure); if($quoteItem = $item->getQuoteItem()){ $item->setSize($quoteItem->getSize()); $item->setEngravingText($quoteItem->getEngravingText()); $item->setEngravingType($quoteItem->getEngravingType()); $item->setProperties($quoteItem->getProperties()); foreach ($quoteItem->getMaterial() as $material) $item->addMaterial($material); } } $showroom = $this->entityManager->getRepository(Showroom::class)->find(1); $order->setShowroom($showroom); }}