Passed
Pull Request — master (#259)
by Arnaud
07:11
created

MenuProvider::mapRouteParameters()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 21.1875

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 20
ccs 3
cts 12
cp 0.25
crap 21.1875
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Menu\Provider;
6
7
use Knp\Menu\ItemInterface;
8
use Knp\Menu\Provider\MenuProviderInterface;
9
use LAG\AdminBundle\Factory\Configuration\ConfigurationFactoryInterface;
10
use LAG\AdminBundle\Menu\Factory\MenuFactoryInterface;
11
use Symfony\Component\Security\Core\Security;
12
13
/**
14
 * Create a new KNP menu using the MenuConfiguration class to validate provided options.
15
 */
16
class MenuProvider implements MenuProviderInterface
17
{
18
    private array $menuConfigurations;
19
    private MenuFactoryInterface $menuFactory;
20
    private ConfigurationFactoryInterface $configurationFactory;
21
    private Security $security;
22
23
    public function __construct(
24
        array $menuConfigurations,
25
        MenuFactoryInterface $menuFactory,
26
        ConfigurationFactoryInterface $configurationFactory,
27 2
        Security $security
28
    ) {
29
        $this->configurationFactory = $configurationFactory;
30
        $this->menuConfigurations = $menuConfigurations;
31
        $this->security = $security;
32
        $this->menuFactory = $menuFactory;
33
    }
34
35 2
    public function get(string $name, array $options = []): ItemInterface
36 2
    {
37 2
        return $this->menuFactory->create($name, $options);
38 2
    }
39 2
40 2
    public function has(string $name, array $options = []): bool
41 2
    {
42
        if (!\array_key_exists($name, $this->menuConfigurations)) {
43 1
            return false;
44
        }
45 1
        $configuration = $this->configurationFactory->createMenuConfiguration($name, $options);
46 1
47 1
        if (!$configuration->hasPermissions()) {
48 1
            return true;
49
        }
50 1
51
        foreach ($configuration->getPermissions() as $permission) {
52 1
            if (!$this->security->isGranted($permission, $this->security->getUser())) {
53 1
                return false;
54 1
            }
55
        }
56 1
57 1
        return true;
58
    }
59
}
60