src/EventListener/SyncCustomerOnPageLoad.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  4. use Symfony\Component\Messenger\MessageBusInterface;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use App\Message\SyncCustomer;
  7. use Sylius\Component\Core\Storage\CartStorageInterface;
  8. use Sylius\Component\Channel\Context\ChannelContextInterface;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Profiler\Profiler;
  11. /**
  12.  * Force la synchronisation du compte client si plus vieux d'1 jour
  13.  */
  14. final class SyncCustomerOnPageLoad
  15. {
  16.     private $bus;
  17.     private $container;
  18.     private $profiler
  19.     protected $syliusSessionStorage;
  20.     protected $channelContext;
  21.     public function __construct(MessageBusInterface $bus
  22.     ContainerInterface $container
  23.     CartStorageInterface $syliusSessionStorage
  24.     ChannelContextInterface $channelContext,
  25.     ?Profiler $profiler)
  26.     {
  27.         $this->bus $bus;
  28.         $this->container $container;
  29.         $this->profiler $profiler;
  30.         $this->syliusSessionStorage $syliusSessionStorage;
  31.         $this->channelContext $channelContext;
  32.     }
  33.     public function onKernelController(ControllerEvent $event): void
  34.     {
  35.         if ($event->isMainRequest()) {
  36.             $customer $this->container->get('sylius.context.customer')->getCustomer();
  37.             // if($this->container->get('security.token_storage')->getToken()->isAuthenticated() 
  38.             // && $this->container->get('security.token_storage')->getToken()->getFireWallName() == 'admin') {
  39.             //     $this->profiler->enable();
  40.             // }
  41.             // else {
  42.             //     $this->profiler->disable();
  43.             // }
  44.             
  45.             // dump($this->container->get('security.token_storage')->getToken()->isAuthenticated());
  46.             // dump($this->container->get('security.token_storage')->getToken()->getFireWallName());
  47.             if(!empty($customer)) {
  48.                 $dateyesterday = (new \DateTime("now"))->sub(new \DateInterval('P1D'));
  49.                 if($customer->getUpdatedAt() < $dateyesterday) {
  50.                     $customer->setUpdatedAt(new \DateTime());
  51.                     $customerManager $this->container->get('sylius.manager.customer');
  52.                     $customerManager->persist($customer);
  53.                     $customerManager->flush();
  54.                     // $this->bus->dispatch(new SyncCustomer((int)$customer->getId()));
  55.                 }
  56.                 //Gestion des paniers utilisateur
  57.                 $currentCart $this->container->get('sylius.context.cart')->getCart();  
  58.                 // $previousCarts
  59.                 // dump($currentCart);
  60.                 $allCarts $this->container->get('sylius.repository.order')->findBy([
  61.                     'customer' => $customer,
  62.                     'state' => 'cart'
  63.                 ], ['id' => 'DESC']);
  64.                 $lastCart $currentCart;
  65.                 if(count($allCarts) > 1) {
  66.                     //On supprime les autres paniers, on ne grade que le dernier (sauf si vide?)
  67.                     $lastCart null;
  68.                     foreach($allCarts as $cart) {
  69.                         if(empty($lastCart) && count($cart->getItems()) > 0) {
  70.                             $lastCart $cart;
  71.                         }
  72.                     }
  73.                     
  74.                     if(!empty($lastCart) && $lastCart->getId() != $currentCart->getId()) {
  75.                         //On switch, car on a un nouveau panier plus rĂ©cent
  76.                         // $this->container->get('sylius.context.cart')->setCart($lastCart);
  77.                         $this->syliusSessionStorage->setForChannel(
  78.                             $this->channelContext->getChannel(),
  79.                             $lastCart
  80.                         );
  81.                     }
  82.                 }
  83.                 
  84.                 // foreach() {
  85.                 // }
  86.                 // dump($lastCart);
  87.                 // $this->session->set('_sylius.cart.ppmc', $lastCart->getId());
  88.                 // _sylius.cart.
  89.                 // $this->container->get('sylius.context.cart')->addContext($lastCart, -1);
  90.                 // $this->container->get('sylius.context.cart')->setCart($lastCart);
  91.                 // $this->container->get('sylius.context.cart')->setCart($cart);
  92.                 // $currentcart = $this->container->get('sylius.context.cart')->getCart();  
  93.                 
  94.                 
  95.                 // dump($currentcart);
  96.                 // exit();
  97.                 // $carts = $customer->getCarts
  98.             }
  99.             //On check les cart du user
  100.             
  101.             // dump($event);
  102.         }
  103.         
  104.         
  105.     }
  106. }