Completed
Pull Request — 4.0 (#4057)
by k-yamamura
07:25
created

MaintenanceListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onKernelRequest() 0 29 5
A getSubscribedEvents() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\EventListener;
15
16
use Eccube\Service\SystemService;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpFoundation\Session\Session;
20
use Symfony\Component\HttpFoundation\Session\SessionInterface;
21
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
24
/**
25
 * メンテナンス管理制御のためのListener
26
 */
27
class MaintenanceListener implements EventSubscriberInterface
28
{
29
30
    /**
31
     * @var Session
32
     */
33
    protected $session;
34
35
    /**
36
     * @var \Twig_Environment
37
     */
38
    protected $twig;
39
40
    /**
41
     * @var SystemService
42
     */
43
    protected $systemService;
44
45
    /**
46
     * MaintenanceListener constructor.
47
     *
48
     * @param SessionInterface $session
49
     * @param \Twig_Environment $twig
50
     * @param SystemService $systemService
51
     */
52
    public function __construct(SessionInterface $session, \Twig_Environment $twig, SystemService $systemService)
53
    {
54
        $this->session = $session;
0 ignored issues
show
Documentation Bug introduced by
$session is of type object<Symfony\Component...ssion\SessionInterface>, but the property $session was declared to be of type object<Symfony\Component...dation\Session\Session>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
55
        $this->twig = $twig;
56
        $this->systemService = $systemService;
57
    }
58
59
60
    /**
61
     * Kernel request listener callback.
62
     *
63
     * @param GetResponseEvent $event
64
     */
65
    public function onKernelRequest(GetResponseEvent $event)
66
    {
67
        if (!$event->isMasterRequest()) {
68
            return;
69
        }
70
71
        $isMaintenance = $this->systemService->isMaintenanceMode();
72
        if (!$isMaintenance) {
73
            return;
74
        }
75
76
        // メンテナンス中であればメンテナンス画面を表示させるが、管理者としてログインされていればフロント画面を表示させる
77
        $is_admin = $this->session->has('_security_admin');
78
        if (!$is_admin) {
79
            $request = $event->getRequest();
80
81
            $pathInfo = \rawurldecode($request->getPathInfo());
82
            $adminPath = env('ECCUBE_ADMIN_ROUTE', 'admin');
83
            $adminPath = '/'.\trim($adminPath, '/').'/';
84
            if (\strpos($pathInfo, $adminPath) !== 0) {
85
                $twig = $this->twig->render('maintenance.twig');
86
                $response = new Response($twig, Response::HTTP_SERVICE_UNAVAILABLE);
87
                $event->setResponse($response);
88
                $event->stopPropagation();
89
            }
90
91
        }
92
93
    }
94
95
    /**
96
     * Return the events to subscribe to.
97
     *
98
     * @return array
99
     */
100
    public static function getSubscribedEvents()
101
    {
102
        return [
103
            KernelEvents::REQUEST => 'onKernelRequest',
104
        ];
105
    }
106
}
107