<?php
/*
* Plugin Name : PointExpired
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\PointExpired42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class KernelEvent implements EventSubscriberInterface
{
private $entityManager;
private $authorizationChecker;
private $tokenStorage;
public function __construct(
EntityManagerInterface $entityManager,
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $tokenStorage
)
{
$this->entityManager = $entityManager;
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'checkPointExpired',
];
}
public function checkPointExpired(ControllerEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
$attributes = $request->attributes;
$route = $attributes->get('_route');
$checkList = ['homepage','mypage','cart','shopping'];
if (in_array($route, $checkList)){
if ($this->authorizationChecker->isGranted('ROLE_USER')) {
$Customer = $this->tokenStorage->getToken()->getUser();
if($Customer instanceof \Eccube\Entity\Customer){
$expiredDate = $Customer->getPointExpiredDate();
if($expiredDate instanceof \DateTime){
$now = new \DateTime();
if($expiredDate < $now){
$Customer->setPoint(0);
$this->entityManager->persist($Customer);
$this->entityManager->flush($Customer);
}
}
}
}
}
}
}