src/EventSubscriber/AddInfoToOrderSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\ItemType;
  5. use App\Entity\Order;
  6. use App\Entity\Showroom;
  7. use App\Entity\TaxRate;
  8. use App\Entity\User;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. final class AddInfoToOrderSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var TokenStorageInterface
  19.      */
  20.     private $tokenStorage;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $entityManager;
  25.     public function __construct(TokenStorageInterface $tokenStorageEntityManagerInterface $entityManager)
  26.     {
  27.         $this->tokenStorage $tokenStorage;
  28.         $this->entityManager $entityManager;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             KernelEvents::VIEW => ['prewrite'EventPriorities::PRE_WRITE],
  34.         ];
  35.     }
  36.     public function prewrite(ViewEvent $event)
  37.     {
  38.         $order $event->getControllerResult();
  39.         $method $event->getRequest()->getMethod();
  40.         if (!$order instanceof Order || Request::METHOD_POST !== $method) {
  41.             // Only handle Order entities (Event is called on any Api entity)
  42.             return;
  43.         }
  44.         // maybe these extra null checks are not even needed
  45.         $token $this->tokenStorage->getToken();
  46.         if (!$token) {
  47.             return;
  48.         }
  49.         $owner $token->getUser();
  50.         if (!$owner instanceof User) {
  51.             return;
  52.         }
  53.         // Attach the user to the not yet persisted Article
  54.         $order->setOwner($owner);
  55.         /*
  56.          * SI prestaCarrier == 17    Retrait boutique        Showroom Bordeaux - 12 Place Gambetta
  57.          * Remplir l'adresse de livraison avec le showroom de bordeaux
  58.          * */
  59.         if($order->getPrestaCarrier() == 17){
  60.             $order->setShippingAddressAddress("RETRAIT SHOWROOM BORDEAUX");
  61.             $order->setShippingAddressAddress2("");
  62.             $order->setShippingAddressCity("BORDEAUX");
  63.             $order->setShippingAddressState(" ");
  64.             $order->setShippingAddressFirstname(" ");
  65.             $order->setShippingAddressLastname(" ");
  66.             $order->setShippingAddressCompany("LE JOAILLIER DU MARAIS BORDEAUX");
  67.             $order->setShippingAddressZipcode("33000");
  68.         }
  69.         /*
  70.          * SI prestaCarrier == 13    Retrait boutique        Showroom Paris - 30 rue RĂ©aumur
  71.          * Remplir l'adresse de livraison avec le showroom de paris
  72.          * */
  73.         if($order->getPrestaCarrier() == 13){
  74.             $order->setShippingAddressAddress("RETRAIT SHOWROOM PARIS");
  75.             $order->setShippingAddressAddress2("");
  76.             $order->setShippingAddressCity("PARIS");
  77.             $order->setShippingAddressState(" ");
  78.             $order->setShippingAddressZipcode("75003");
  79.             $order->setShippingAddressFirstname(" ");
  80.             $order->setShippingAddressLastname(" ");
  81.             $order->setShippingAddressCompany("LE JOAILLIER DU MARAIS PARIS");
  82.         }
  83.         $taxRate $this->entityManager->getRepository(TaxRate::class)->findOneBy(["def" => 1]);
  84.         $typesurmesure $this->entityManager->getRepository(ItemType::class)->find(1);
  85.         //RECUPERATION DES INFOS DU QUOTE ITEM
  86.         foreach ($order->getItem() as $item) {
  87.             $item->setTaxRate($taxRate);
  88.             $item->setType($typesurmesure);
  89.             if($quoteItem $item->getQuoteItem()){
  90.                 $item->setSize($quoteItem->getSize());
  91.                 $item->setEngravingText($quoteItem->getEngravingText());
  92.                 $item->setEngravingType($quoteItem->getEngravingType());
  93.                 $item->setProperties($quoteItem->getProperties());
  94.                 foreach ($quoteItem->getMaterial() as $material)
  95.                     $item->addMaterial($material);
  96.             }
  97.         }
  98.         $showroom $this->entityManager->getRepository(Showroom::class)->find(1);
  99.         $order->setShowroom($showroom);
  100.     }
  101. }