<?php
namespace Plugin\NZCustomPlugin\EventListener;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RegistrationEventListener implements EventSubscriberInterface
{
private $session;
private $router;
public function __construct(
SessionInterface $session,
RouterInterface $router
) {
$this->session = $session;
$this->router = $router;
}
public static function getSubscribedEvents()
{
return [
EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => 'onRegistrationComplete',
'kernel.response' => 'onKernelResponse',
];
}
/**
* 会員登録完了時の処理
*/
public function onRegistrationComplete(EventArgs $event)
{
// セッションからリダイレクト先フォームキーを取得
$formKey = $this->session->get('redirect_to_form');
if ($formKey) {
// 仮登録完了フラグを設定
$this->session->set('eccube.front.entry.complete', true);
// リダイレクトURLを生成(フォームの説明画面へ)
$redirectUrl = $this->router->generate('nzcustomplugin_form_display', ['key' => $formKey]);
// レスポンスをリダイレクトに変更
$response = new RedirectResponse($redirectUrl);
$event->setResponse($response);
}
}
/**
* 会員登録完了ページからのリダイレクト処理
*/
public function onKernelResponse(ResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
$route = $request->attributes->get('_route');
// 会員登録完了ページの場合
if ($route === 'entry_complete') {
$formKey = $this->session->get('redirect_to_form');
if ($formKey) {
// 仮登録完了フラグを設定
$this->session->set('eccube.front.entry.complete', true);
// フォーム画面へリダイレクト
$redirectUrl = $this->router->generate('nzcustomplugin_form_display', ['key' => $formKey]);
$response = new RedirectResponse($redirectUrl);
$event->setResponse($response);
}
}
}
}