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

AdminVoter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequest() 0 6 1
C matchItem() 0 28 11
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