Failed Conditions
Pull Request — 4.0 (#4057)
by k-yamamura
05:56
created

MaintenanceListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Session\Session;
19
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
21
use Symfony\Component\HttpKernel\KernelEvents;
22
23
/**
24
 * メンテナンス管理制御のためのListener
25
 */
26
class MaintenanceListener implements EventSubscriberInterface
27
{
28
29
    /**
30
     * @var Session
31
     */
32
    protected $session;
33
34
    /**
35
     * @var SystemService
36
     */
37
    protected $systemService;
38
39
    /**
40
     * MaintenanceListener constructor.
41
     *
42
     * @param SessionInterface $session
43
     * @param SystemService $systemService
44
     */
45
    public function __construct(SessionInterface $session, SystemService $systemService)
46
    {
47
        $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...
48
        $this->systemService = $systemService;
49
    }
50
51
52
    /**
53
     * Kernel request listener callback.
54
     *
55
     * @param GetResponseEvent $event
56
     */
57
    public function onKernelRequest(GetResponseEvent $event)
58
    {
59
        if (!$event->isMasterRequest()) {
60
            return;
61
        }
62
63
        $isMaintenance = $this->systemService->isMaintenanceMode();
64
65
        if ($isMaintenance) {
66
67
            // メンテナンス中であればメンテナンス画面を表示させるが、管理者としてログインされていればフロント画面を表示させる
68
            $is_admin = $this->session->has('_security_admin');
69
            if (!$is_admin) {
70
                $request = $event->getRequest();
71
72
                $pathInfo = \rawurldecode($request->getPathInfo());
73
                $adminPath = env('ECCUBE_ADMIN_ROUTE', 'admin');
74
                $adminPath = '/'.\trim($adminPath, '/').'/';
75
                if (\strpos($pathInfo, $adminPath) !== 0) {
76
                    $locale = env('ECCUBE_LOCALE');
77
                    $templateCode = env('ECCUBE_TEMPLATE_CODE');
78
                    $baseUrl = \htmlspecialchars(\rawurldecode($request->getBaseUrl()), ENT_QUOTES);
79
80
                    header('HTTP/1.1 503 Service Temporarily Unavailable');
81
                    require __DIR__.'/../../../maintenance.php';
82
                    exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method onKernelRequest() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
83
                }
84
85
            }
86
        }
87
88
    }
89
90
    /**
91
     * Return the events to subscribe to.
92
     *
93
     * @return array
94
     */
95
    public static function getSubscribedEvents()
96
    {
97
        return [
98
            KernelEvents::REQUEST => 'onKernelRequest',
99
        ];
100
    }
101
}
102