Completed
Push — 3.x ( bf82cf...39bdd5 )
by Grégoire
04:22
created

AdminVoter::matchItem()   C

Complexity

Conditions 11
Paths 6

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 15
nc 6
nop 1

How to fix   Complexity   

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 the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Menu\Matcher\Voter;
13
14
use Knp\Menu\ItemInterface;
15
use Knp\Menu\Matcher\Voter\VoterInterface;
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Admin menu voter based on extra `admin`.
21
 *
22
 * @author Samusev Andrey <[email protected]>
23
 */
24
class AdminVoter implements VoterInterface
25
{
26
    /**
27
     * @var Request
28
     */
29
    private $request = null;
30
31
    /**
32
     * @param Request $request
33
     *
34
     * @return $this
35
     */
36
    public function setRequest($request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
37
    {
38
        $this->request = $request;
39
40
        return $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function matchItem(ItemInterface $item)
47
    {
48
        $admin = $item->getExtra('admin');
49
50
        if ($admin instanceof AdminInterface
51
            && $admin->hasRoute('list') && $admin->hasAccess('list')
52
            && $this->request
53
        ) {
54
            $requestCode = $this->request->get('_sonata_admin');
55
56
            if ($admin->getCode() === $requestCode) {
57
                return true;
58
            }
59
60
            foreach ($admin->getChildren() as $child) {
61
                if ($child->getBaseCodeRoute() === $requestCode) {
62
                    return true;
63
                }
64
            }
65
        }
66
67
        $route = $item->getExtra('route');
68
        if ($route && $this->request && $route == $this->request->get('_route')) {
69
            return true;
70
        }
71
72
        return null;
73
    }
74
}
75