<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\EventListener;
use Customize\Service\MailMagazineService;
use Eccube\Event\EventArgs;
use Eccube\Request\Context;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Customize\Event\LmEvents;
/**
* @inheritDoc
*/
class MailMagazineEventListener implements EventSubscriberInterface
{
/**
* @var Context
*/
private $requestContext;
/**
* @var MailMagazineService
*/
private $mailMagazineService;
/**
* @inheritDoc
*/
public function __construct(
Context $context,
MailMagazineService $mailMagazineService
) {
$this->requestContext = $context;
$this->mailMagazineService = $mailMagazineService;
}
public function onKernelRequest(ResponseEvent $event)
{
if ($event->isMasterRequest()) {
if ($this->requestContext->isFront()) {
if ($hashKey = $this->mailMagazineService->retrieveHashKey($event->getRequest())) {
// メールマガジンが開封されたイベントを発火
$this->mailMagazineService->fireOpenedIfSo($event->getRequest(), $hashKey);
// メールマガジンのリンクがクリックされたイベントを発火
$this->mailMagazineService->fireClickedIfSo($event->getRequest(), $hashKey);
//
$cookie = new Cookie(MailMagazineService::COOKIE_NAME, $hashKey, time() + MailMagazineService::COOKIE_EXPIRES, MailMagazineService::COOKIE_PATH);
$response = $event->getResponse();
$response->headers->setCookie($cookie);
$event->setResponse($response);
}
}
}
}
/**
* メールマガジンのリンクが開封されたイベント
*
* @param EventArgs $event
* @return void
*/
public function onMailMagazineOpened(EventArgs $event)
{
if ($event->hasArgument('md_hash_key') && $hashKey = $event->getArgument('md_hash_key')) {
//
$this->mailMagazineService->saveMailMagazineAsOpened($hashKey);
}
}
/**
* メールマガジンのリンクがクリックされたイベント
*
* @param EventArgs $event
* @return void
*/
public function onMailMagazineClicked(EventArgs $event)
{
if ($event->hasArgument('md_hash_key') && $hashKey = $event->getArgument('md_hash_key')) {
//
$this->mailMagazineService->saveMailMagazineAsClicked($hashKey);
}
}
/**
* メールマガジンの購読が中止されたイベント
*
* @param EventArgs $event
* @return void
*/
public function onMailMagazineAborted(EventArgs $event)
{
if ($event->hasArgument('md_hash_key') && $hashKey = $event->getArgument('md_hash_key')) {
//
$this->mailMagazineService->saveMailMagazineAsAborted($hashKey);
}
}
/**
* メールマガジンのリンクがコンバージョンされたイベント
*
* @param EventArgs $event
* @return void
*/
public function onMailMagazineConverted(EventArgs $event)
{
if ($event->hasArgument('md_hash_key') && $hashKey = $event->getArgument('md_hash_key')) {
//
$this->mailMagazineService->saveMailMagazineAsConverted($hashKey);
}
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
// SecurityServiceProviderで、認証処理が完了した後に実行.
['onKernelRequest', 6],
],
LmEvents::MAIL_MAGAZINE_OPENED => 'onMailMagazineOpened',
LmEvents::MAIL_MAGAZINE_CLICKED => 'onMailMagazineClicked',
LmEvents::MAIL_MAGAZINE_ABORTED => 'onMailMagazineAborted',
LmEvents::MAIL_MAGAZINE_CONVERTED => 'onMailMagazineConverted',
];
}
}