<?php
namespace Plugin\GoToCartB;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Service\CartService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class GoToCartBEvent implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [KernelEvents::RESPONSE => 'goToCart'];
}
public function __construct(CartService $cartService, BaseInfoRepository $baseInfoRepository)
{
$this->cartService = $cartService;
$this->baseInfo = $baseInfoRepository->get();
}
public function goToCart(FilterResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
$attributes = $request->attributes;
$route = $attributes->get('_route');
$activateFlg = $this->baseInfo->isOptionCustomerActivate();
if ($route == 'entry_activate' && $activateFlg == false) {
$Carts = $this->cartService->getCarts();
$quantityInCart = 0;
foreach ($Carts as $cart) {
$quantityInCart += $cart->getTotalQuantity();
}
if ($quantityInCart > 0) {
$event->setResponse(new RedirectResponse('/cart'));
}
}
}
}