RegisterMenuPass::process()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4
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