<?php
declare(strict_types=1);
namespace App\Authentication;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Http\HttpUtils;
use Psr\Log\LoggerInterface;
use App\Message\SyncCustomer;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
{
private $bus;
public function __construct(HttpUtils $httpUtils, array $options = [], LoggerInterface $logger = null, RequestStack $request, MessageBusInterface $bus, ContainerInterface $container)
{
$this->httpUtils = $httpUtils;
$this->logger = $logger;
$this->setOptions($options);
$this->bus = $bus;
$this->container = $container;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token): Response
{
//On lance la synchronisation du compte client
// dump($token->getUser()->getCustomer());exit();
$this->bus->dispatch(new SyncCustomer((int)$token->getUser()->getCustomer()->getId()));
if ($request->isXmlHttpRequest()) {
return new JsonResponse(['success' => true, 'username' => $token->getUser()->getUsername()]);
}
$cart = $this->container->get('sylius.context.cart')->getCart();
if (null !== $cart->getId()) {
return $this->httpUtils->createRedirectResponse($request, 'sylius_shop_cart_summary');
}else{
return $this->httpUtils->createRedirectResponse($request, 'sylius_shop_account_dashboard');
}
return $this->httpUtils->createRedirectResponse($request, 'sylius_shop_cart_summary');
// $syliusTargetPath = $request->getSession()->get('_security.shop.target_path', null);
// if (empty($syliusTargetPath) == false) {
// return $this->httpUtils->createRedirectResponse($request, $syliusTargetPath);
// }
// return parent::onAuthenticationSuccess($request, $token);
}
}