<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use App\Message\SyncCustomer;
use Sylius\Component\Core\Storage\CartStorageInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Profiler\Profiler;
/**
* Force la synchronisation du compte client si plus vieux d'1 jour
*/
final class SyncCustomerOnPageLoad
{
private $bus;
private $container;
private $profiler;
protected $syliusSessionStorage;
protected $channelContext;
public function __construct(MessageBusInterface $bus,
ContainerInterface $container,
CartStorageInterface $syliusSessionStorage,
ChannelContextInterface $channelContext,
?Profiler $profiler)
{
$this->bus = $bus;
$this->container = $container;
$this->profiler = $profiler;
$this->syliusSessionStorage = $syliusSessionStorage;
$this->channelContext = $channelContext;
}
public function onKernelController(ControllerEvent $event): void
{
if ($event->isMainRequest()) {
$customer = $this->container->get('sylius.context.customer')->getCustomer();
// if($this->container->get('security.token_storage')->getToken()->isAuthenticated()
// && $this->container->get('security.token_storage')->getToken()->getFireWallName() == 'admin') {
// $this->profiler->enable();
// }
// else {
// $this->profiler->disable();
// }
// dump($this->container->get('security.token_storage')->getToken()->isAuthenticated());
// dump($this->container->get('security.token_storage')->getToken()->getFireWallName());
if(!empty($customer)) {
$dateyesterday = (new \DateTime("now"))->sub(new \DateInterval('P1D'));
if($customer->getUpdatedAt() < $dateyesterday) {
$customer->setUpdatedAt(new \DateTime());
$customerManager = $this->container->get('sylius.manager.customer');
$customerManager->persist($customer);
$customerManager->flush();
// $this->bus->dispatch(new SyncCustomer((int)$customer->getId()));
}
//Gestion des paniers utilisateur
$currentCart = $this->container->get('sylius.context.cart')->getCart();
// $previousCarts
// dump($currentCart);
$allCarts = $this->container->get('sylius.repository.order')->findBy([
'customer' => $customer,
'state' => 'cart'
], ['id' => 'DESC']);
$lastCart = $currentCart;
if(count($allCarts) > 1) {
//On supprime les autres paniers, on ne grade que le dernier (sauf si vide?)
$lastCart = null;
foreach($allCarts as $cart) {
if(empty($lastCart) && count($cart->getItems()) > 0) {
$lastCart = $cart;
}
}
if(!empty($lastCart) && $lastCart->getId() != $currentCart->getId()) {
//On switch, car on a un nouveau panier plus récent
// $this->container->get('sylius.context.cart')->setCart($lastCart);
$this->syliusSessionStorage->setForChannel(
$this->channelContext->getChannel(),
$lastCart
);
}
}
// foreach() {
// }
// dump($lastCart);
// $this->session->set('_sylius.cart.ppmc', $lastCart->getId());
// _sylius.cart.
// $this->container->get('sylius.context.cart')->addContext($lastCart, -1);
// $this->container->get('sylius.context.cart')->setCart($lastCart);
// $this->container->get('sylius.context.cart')->setCart($cart);
// $currentcart = $this->container->get('sylius.context.cart')->getCart();
// dump($currentcart);
// exit();
// $carts = $customer->getCarts
}
//On check les cart du user
// dump($event);
}
}
}