app/Plugin/PointExpired42/Event/KernelEvent.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : PointExpired
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\PointExpired42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  18. class KernelEvent implements EventSubscriberInterface
  19. {
  20.     private $entityManager;
  21.     private $authorizationChecker;
  22.     private $tokenStorage;
  23.     public function __construct(
  24.             EntityManagerInterface $entityManager,
  25.             AuthorizationCheckerInterface $authorizationChecker,
  26.             TokenStorageInterface $tokenStorage
  27.             )
  28.     {
  29.         $this->entityManager $entityManager;
  30.         $this->authorizationChecker $authorizationChecker;
  31.         $this->tokenStorage $tokenStorage;
  32.     }
  33.     /**
  34.      * @return array
  35.      */
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             KernelEvents::CONTROLLER => 'checkPointExpired',
  40.                 ];
  41.     }
  42.     public function checkPointExpired(ControllerEvent $event)
  43.     {
  44.         if (!$event->isMasterRequest()) {
  45.             return;
  46.         }
  47.         $request $event->getRequest();
  48.         $attributes $request->attributes;
  49.         $route $attributes->get('_route');
  50.         $checkList = ['homepage','mypage','cart','shopping'];
  51.         if (in_array($route$checkList)){
  52.             if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  53.                 $Customer $this->tokenStorage->getToken()->getUser();
  54.                 if($Customer instanceof \Eccube\Entity\Customer){
  55.                     $expiredDate $Customer->getPointExpiredDate();
  56.                     if($expiredDate instanceof \DateTime){
  57.                         $now = new \DateTime();
  58.                         if($expiredDate $now){
  59.                             $Customer->setPoint(0);
  60.                             $this->entityManager->persist($Customer);
  61.                             $this->entityManager->flush($Customer);
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }