src/Eccube/EventListener/MaintenanceListener.php line 46

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\EventListener;
  13. use Eccube\Entity;
  14. use Eccube\Request\Context;
  15. use Eccube\Service\SystemService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Cookie;
  18. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. class MaintenanceListener implements EventSubscriberInterface
  21. {
  22.     /** @var Context */
  23.     protected $requestContext;
  24.     /** @var SystemService */
  25.     protected $systemService;
  26.     public function __construct(Context $requestContextSystemService $systemService)
  27.     {
  28.         $this->requestContext $requestContext;
  29.         $this->systemService $systemService;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             KernelEvents::RESPONSE => ['onResponse'],
  35.         ];
  36.     }
  37.     public function onResponse(FilterResponseEvent $event)
  38.     {
  39.         $response $event->getResponse();
  40.         if (!$this->systemService->isMaintenanceMode()) {
  41.             $response->headers->clearCookie(SystemService::MAINTENANCE_TOKEN_KEY);
  42.             return;
  43.         }
  44.         $user $this->requestContext->getCurrentUser();
  45.         if ($user instanceof Entity\Member && $this->requestContext->isAdmin()) {
  46.             $cookie = new Cookie(
  47.                 SystemService::MAINTENANCE_TOKEN_KEY,
  48.                 $this->systemService->getMaintenanceToken()
  49.             );
  50.             $response->headers->setCookie($cookie);
  51.         }
  52.     }
  53. }