<?php
namespace Plugin\ShippingRedirect;
use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class Event implements EventSubscriberInterface
{
protected $router;
protected $redirect_url;
public function __construct(RouterInterface $router) {
$this->router = $router;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Shopping/index.twig' => 'index',
KernelEvents::RESPONSE => 'onResponse',
];
}
/**
* @param TemplateEvent $event
*/
public function index(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Order = $parameters['Order'];
// shippingIdを取得して、リダイレクト先にshopping_shipping_editを設定する
$this->redirect_url = $this->router->generate('shopping_shipping_edit', ['id' => $Order->getShippings()->current()->getId()]);
}
public function onResponse(ResponseEvent $event)
{
// shopping_nonmember のフルパスのURLを取得
$url_shopping_nonmember = $this->router->generate('shopping_nonmember', [], UrlGeneratorInterface::ABSOLUTE_URL);
// Requestオブジェクトを取得
$request = $event->getRequest();
// リファラを取得
$referrer = $request->headers->get('referer');
// リファラが shopping_nonmember の場合、$this->redirect_url にリダイレクトする
if($referrer === $url_shopping_nonmember && isset($this->redirect_url)){
$response = new RedirectResponse($this->redirect_url);
$event->setResponse($response);
}
}
}