Completed
Push — experimental/sf ( b34b7c...088e42 )
by Ryo
317:30 queued 252:30
created

TwigInitializeListener::setFrontVaribales()   B

Complexity

Conditions 6
Paths 30

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.0029

Importance

Changes 0
Metric Value
cc 6
nc 30
nop 1
dl 0
loc 31
ccs 22
cts 23
cp 0.9565
crap 6.0029
rs 8.8017
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 Doctrine\ORM\NoResultException;
17
use Eccube\Common\EccubeConfig;
18
use Eccube\Entity\AuthorityRole;
19
use Eccube\Entity\Master\DeviceType;
20
use Eccube\Entity\Member;
21
use Eccube\Repository\AuthorityRoleRepository;
22
use Eccube\Repository\BaseInfoRepository;
23
use Eccube\Repository\Master\DeviceTypeRepository;
24
use Eccube\Repository\PageRepository;
25
use Eccube\Request\Context;
26
use SunCat\MobileDetectBundle\DeviceDetector\MobileDetector;
27
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
28
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
29
use Symfony\Component\HttpKernel\KernelEvents;
30
use Twig\Environment;
31
32
class TwigInitializeListener implements EventSubscriberInterface
33
{
34
    /**
35
     * @var Environment
36
     */
37
    protected $twig;
38
39
    /**
40
     * @var BaseInfoRepository
41
     */
42
    protected $baseInfoRepository;
43
44
    /**
45
     * @var DeviceTypeRepository
46
     */
47
    protected $deviceTypeRepository;
48
49
    /**
50
     * @var PageRepository
51
     */
52
    protected $pageRepository;
53
54
    /**
55
     * @var Context
56
     */
57
    protected $requestContext;
58
59
    /**
60
     * @var AuthorityRoleRepository
61
     */
62
    private $authorityRoleRepository;
63
64
    /**
65
     * @var EccubeConfig
66
     */
67
    private $eccubeConfig;
68
69
    /**
70
     * @var MobileDetector
71
     */
72
    private $mobileDetector;
73
74
    /**
75
     * TwigInitializeListener constructor.
76
     *
77
     * @param Environment $twig
78
     * @param BaseInfoRepository $baseInfoRepository
79
     * @param PageRepository $pageRepository
80
     * @param DeviceTypeRepository $deviceTypeRepository
81
     * @param AuthorityRoleRepository $authorityRoleRepository
82
     * @param EccubeConfig $eccubeConfig
83
     * @param Context $context
84
     * @param MobileDetector $mobileDetector
85
     */
86 436
    public function __construct(
87
        Environment $twig,
88
        BaseInfoRepository $baseInfoRepository,
89
        PageRepository $pageRepository,
90
        DeviceTypeRepository $deviceTypeRepository,
91
        AuthorityRoleRepository $authorityRoleRepository,
92
        EccubeConfig $eccubeConfig,
93
        Context $context,
94
        MobileDetector $mobileDetector
95
    ) {
96 436
        $this->twig = $twig;
97 436
        $this->baseInfoRepository = $baseInfoRepository;
98 436
        $this->pageRepository = $pageRepository;
99 436
        $this->deviceTypeRepository = $deviceTypeRepository;
100 436
        $this->authorityRoleRepository = $authorityRoleRepository;
101 436
        $this->eccubeConfig = $eccubeConfig;
102 436
        $this->requestContext = $context;
103 436
        $this->mobileDetector = $mobileDetector;
104
    }
105
106
    /**
107
     * @param GetResponseEvent $event
108
     *
109
     * @throws NoResultException
110
     * @throws \Doctrine\ORM\NonUniqueResultException
111
     */
112 434
    public function onKernelRequest(GetResponseEvent $event)
113
    {
114 434
        $globals = $this->twig->getGlobals();
115 434
        if (array_key_exists('BaseInfo', $globals) && $globals['BaseInfo'] === null) {
116 434
            $this->twig->addGlobal('BaseInfo', $this->baseInfoRepository->get());
117
        }
118
119 434
        if (!$event->isMasterRequest()) {
120 118
            return;
121
        }
122
123 433
        if ($this->requestContext->isAdmin()) {
124 275
            $this->setAdminGlobals($event);
125
        } else {
126 158
            $this->setFrontVaribales($event);
127
        }
128
    }
129
130
    /**
131
     * @param GetResponseEvent $event
132
     *
133
     * @throws \Doctrine\ORM\NonUniqueResultException
134
     */
135 158
    public function setFrontVaribales(GetResponseEvent $event)
136
    {
137
        /** @var \Symfony\Component\HttpFoundation\ParameterBag $attributes */
138 158
        $attributes = $event->getRequest()->attributes;
139 158
        $route = $attributes->get('_route');
140 158
        if ($route == 'user_data') {
141 2
            $routeParams = $attributes->get('_route_params', []);
142 2
            $route = isset($routeParams['route']) ? $routeParams['route'] : $attributes->get('route', '');
143
        }
144
145 158
        $type = DeviceType::DEVICE_TYPE_PC;
146 158
        if ($this->mobileDetector->isMobile()) {
147
            $type = DeviceType::DEVICE_TYPE_SP;
148
        }
149
        $DeviceType = $this->deviceTypeRepository->find($type);
150 158
151
        try {
152
            $Page = $this->pageRepository->getByUrl($DeviceType, $route);
0 ignored issues
show
Documentation introduced by
$DeviceType is of type object|null, but the function expects a object<Eccube\Entity\Master\DeviceType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153 158
        } catch (NoResultException $e) {
154 158
            try {
155 158
                log_info('fallback to PC layout');
156 158
                $DeviceType = $this->deviceTypeRepository->find(DeviceType::DEVICE_TYPE_PC);
157 158
                $Page = $this->pageRepository->getByUrl($DeviceType, $route);
0 ignored issues
show
Documentation introduced by
$DeviceType is of type object|null, but the function expects a object<Eccube\Entity\Master\DeviceType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158 158
            } catch (NoResultException $e) {
159 158
                $Page = $this->pageRepository->newPage($DeviceType);
0 ignored issues
show
Documentation introduced by
$DeviceType is of type object|null, but the function expects a object<Eccube\Entity\Master\DeviceType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160 158
            }
161 158
        }
162 158
163 158
        $this->twig->addGlobal('Page', $Page);
164 158
        $this->twig->addGlobal('title', $Page->getName());
165 158
    }
166 92
167 92
    /**
168
     * @param GetResponseEvent $event
169
     */
170 158
    public function setAdminGlobals(GetResponseEvent $event)
171 148
    {
172
        // メニュー表示用配列.
173
        $menus = [];
174
        $this->twig->addGlobal('menus', $menus);
175
176
        // メニューの権限制御.
177 275
        $Member = $this->requestContext->getCurrentUser();
178
        $AuthorityRoles = [];
179
        if ($Member instanceof Member) {
180 275
            $AuthorityRoles = $this->authorityRoleRepository->findBy(['Authority' => $this->requestContext->getCurrentUser()->getAuthority()]);
0 ignored issues
show
Bug introduced by
The method getAuthority does only exist in Eccube\Entity\Member, but not in Eccube\Entity\Customer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
181 275
        }
182
        $roles = array_map(function (AuthorityRole $AuthorityRole) use ($event) {
183
            return $event->getRequest()->getBaseUrl().'/'.$this->eccubeConfig['eccube_admin_route'].$AuthorityRole->getDenyUrl();
184 275
        }, $AuthorityRoles);
185 275
        $this->twig->addGlobal('AuthorityRoles', $roles);
186 275
    }
187 274
188
    /**
189 275
     * {@inheritdoc}
190 3
     */
191 275
    public static function getSubscribedEvents()
192 275
    {
193
        return [
194
            KernelEvents::REQUEST => [
195
                // SecurityServiceProviderで、認証処理が完了した後に実行.
196
                ['onKernelRequest', 6],
197
            ],
198 1
        ];
199
    }
200
}
201