Failed Conditions
Pull Request — experimental/sf (#3445)
by
unknown
206:10 queued 141:04
created

TwigInitializeListener::setFrontVaribales()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
nc 36
nop 1
dl 0
loc 53
ccs 30
cts 30
cp 1
crap 8
rs 7.781
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Repository\PageLayoutRepository;
26
use Eccube\Repository\BlockPositionRepository;
27
use Eccube\Request\Context;
28
use SunCat\MobileDetectBundle\DeviceDetector\MobileDetector;
29
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
30
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
31
use Symfony\Component\HttpKernel\KernelEvents;
32
use Twig\Environment;
33
34
class TwigInitializeListener implements EventSubscriberInterface
35
{
36
    /**
37
     * @var Environment
38
     */
39
    protected $twig;
40
41
    /**
42
     * @var BaseInfoRepository
43
     */
44
    protected $baseInfoRepository;
45
46
    /**
47
     * @var DeviceTypeRepository
48
     */
49
    protected $deviceTypeRepository;
50
51
    /**
52
     * @var PageRepository
53
     */
54
    protected $pageRepository;
55
56
    /**
57
     * @var PageLayoutRepository
58
     */
59
    protected $pageLayoutRepository;
60
61
    /**
62
     * @var BlockPositionRepository
63
     */
64
    protected $blockPositionRepository;
65
66
    /**
67
     * @var Context
68
     */
69
    protected $requestContext;
70
71
    /**
72
     * @var AuthorityRoleRepository
73
     */
74
    private $authorityRoleRepository;
75
76
    /**
77
     * @var EccubeConfig
78
     */
79
    private $eccubeConfig;
80
81
    /**
82
     * @var MobileDetector
83
     */
84
    private $mobileDetector;
85
86 434
    /**
87
     * TwigInitializeListener constructor.
88
     *
89
     * @param Environment $twig
90
     * @param BaseInfoRepository $baseInfoRepository
91
     * @param PageRepository $pageRepository
92
     * @param PageLayoutRepository $pageLayoutRepository
93
     * @param BlockPositionRepository $blockPositionRepository
94
     * @param DeviceTypeRepository $deviceTypeRepository
95
     * @param AuthorityRoleRepository $authorityRoleRepository
96 434
     * @param EccubeConfig $eccubeConfig
97 434
     * @param Context $context
98 434
     * @param MobileDetector $mobileDetector
99 434
     */
100 434
    public function __construct(
101 434
        Environment $twig,
102 434
        BaseInfoRepository $baseInfoRepository,
103 434
        PageRepository $pageRepository,
104
        PageLayoutRepository $pageLayoutRepository,
105
        BlockPositionRepository $blockPositionRepository,
106
        DeviceTypeRepository $deviceTypeRepository,
107
        AuthorityRoleRepository $authorityRoleRepository,
108
        EccubeConfig $eccubeConfig,
109
        Context $context,
110
        MobileDetector $mobileDetector
111
    ) {
112 432
        $this->twig = $twig;
113
        $this->baseInfoRepository = $baseInfoRepository;
114 432
        $this->pageRepository = $pageRepository;
115 432
        $this->pageLayoutRepository = $pageLayoutRepository;
116 432
        $this->blockPositionRepository = $blockPositionRepository;
117
        $this->deviceTypeRepository = $deviceTypeRepository;
118
        $this->authorityRoleRepository = $authorityRoleRepository;
119 432
        $this->eccubeConfig = $eccubeConfig;
120 118
        $this->requestContext = $context;
121
        $this->mobileDetector = $mobileDetector;
122
    }
123 431
124 273
    /**
125
     * @param GetResponseEvent $event
126 158
     *
127
     * @throws NoResultException
128
     * @throws \Doctrine\ORM\NonUniqueResultException
129
     */
130
    public function onKernelRequest(GetResponseEvent $event)
131
    {
132
        $globals = $this->twig->getGlobals();
133
        if (array_key_exists('BaseInfo', $globals) && $globals['BaseInfo'] === null) {
134
            $this->twig->addGlobal('BaseInfo', $this->baseInfoRepository->get());
135 158
        }
136
137
        if (!$event->isMasterRequest()) {
138 158
            return;
139 158
        }
140 158
141 2
        if ($this->requestContext->isAdmin()) {
142 2
            $this->setAdminGlobals($event);
143
        } else {
144
            $this->setFrontVaribales($event);
145 158
        }
146 158
    }
147
148
    /**
149
     * @param GetResponseEvent $event
150 158
     *
151
     * @throws \Doctrine\ORM\NonUniqueResultException
152
     */
153 158
    public function setFrontVaribales(GetResponseEvent $event)
154 158
    {
155 158
        $request = $event->getRequest();
156 158
        /** @var \Symfony\Component\HttpFoundation\ParameterBag $attributes */
157 158
        $attributes = $request->attributes;
158 158
        $route = $attributes->get('_route');
159 158
        if ($route == 'user_data') {
160 158
            $routeParams = $attributes->get('_route_params', []);
161 158
            $route = isset($routeParams['route']) ? $routeParams['route'] : $attributes->get('route', '');
162 158
        }
163 158
164 158
        $type = DeviceType::DEVICE_TYPE_PC;
165 158
        if ($this->mobileDetector->isMobile()) {
166 92
            $type = DeviceType::DEVICE_TYPE_SP;
167 92
        }
168
169
        $DeviceType = $this->deviceTypeRepository->find($type);
170 158
171 148
        try {
172
            $qb = $this->pageRepository->createQueryBuilder('p');
173
            $Page = $qb->select('p, pll,l, bp, b')
174
                ->leftJoin('p.PageLayouts', 'pll')
175
                ->leftJoin('pll.Layout', 'l')
176
                ->leftJoin('l.BlockPositions', 'bp')
177 273
                ->leftJoin('bp.Block', 'b')
178
                ->where('p.url = :route')
179
                ->andWhere('l.DeviceType = :DeviceType')
180 273
                ->orderBy('bp.block_row', 'ASC')
181 273
                ->setParameter('route', $route)
182
                ->setParameter('DeviceType', $DeviceType)
183
                ->getQuery()
184 273
                ->getSingleResult();
185 273
        } catch (NoResultException $e) {
186 273
            $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...
187 272
        }
188
189 273
        if ($request->get('preview')) {
190 3
            $Page->getPageLayouts()->clear();
191 273
            $PageLayouts = $this->pageLayoutRepository->findBy(['layout_id' => 0]);
192 273
            foreach ($PageLayouts as $PageLayout) {
193
                $Page->addPageLayout($PageLayout);
194
            }
195
196
            $Page->getBlockPositions()->clear();
197
            $BlockPositions = $this->blockPositionRepository->findBy(['layout_id' => 0]);
198 1
            foreach ($BlockPositions as $BlockPosition) {
199
                $Page->addBlockPosition($BlockPosition);
200
            }
201 1
        }
202
203
        $this->twig->addGlobal('Page', $Page);
204
        $this->twig->addGlobal('title', $Page->getName());
205
    }
206
207
    /**
208
     * @param GetResponseEvent $event
209
     */
210
    public function setAdminGlobals(GetResponseEvent $event)
211
    {
212
        // メニュー表示用配列.
213
        $menus = [];
214
        $this->twig->addGlobal('menus', $menus);
215
216
        // メニューの権限制御.
217
        $Member = $this->requestContext->getCurrentUser();
218
        $AuthorityRoles = [];
219
        if ($Member instanceof Member) {
220
            $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...
221
        }
222
        $roles = array_map(function (AuthorityRole $AuthorityRole) use ($event) {
223
            return $event->getRequest()->getBaseUrl().'/'.$this->eccubeConfig['eccube_admin_route'].$AuthorityRole->getDenyUrl();
224
        }, $AuthorityRoles);
225
        $this->twig->addGlobal('AuthorityRoles', $roles);
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public static function getSubscribedEvents()
232
    {
233
        return [
234
            KernelEvents::REQUEST => [
235
                // SecurityServiceProviderで、認証処理が完了した後に実行.
236
                ['onKernelRequest', 6],
237
            ],
238
        ];
239
    }
240
}
241