RegisterMenuPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 4
A createMenuService() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\UiBundle\DependencyInjection\Compiler;
13
14
use Knp\Menu\ItemInterface;
15
use Lug\Bundle\UiBundle\Exception\TagAttributeNotFoundException;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class RegisterMenuPass implements CompilerPassInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function process(ContainerBuilder $container)
30
    {
31 2
        foreach ($container->findTaggedServiceIds($tag = 'lug.menu.builder') as $service => $attributes) {
32 2
            foreach ($attributes as $attribute) {
33 2
                if (!isset($attribute['alias'])) {
34 1
                    throw new TagAttributeNotFoundException(sprintf(
35 1
                        'The attribute "alias" could not be found for the tag "%s" on the "%s" service.',
36 1
                        $tag,
37
                        $service
38 1
                    ));
39
                }
40
41 1
                $container->setDefinition($service.'.menu', $this->createMenuService($service, $attribute['alias']));
42 1
            }
43 1
        }
44 1
    }
45
46
    /**
47
     * @param string $service
48
     * @param string $alias
49
     *
50
     * @return Definition
51
     */
52 1
    private function createMenuService($service, $alias)
53
    {
54 1
        $definition = new Definition(ItemInterface::class);
55 1
        $definition->setFactory([new Reference($service), 'create']);
56 1
        $definition->addTag('knp_menu.menu', ['alias' => $alias]);
57
58 1
        return $definition;
59
    }
60
}
61