ActionRegisterCompilerPass::process()   C
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 42.2105

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
ccs 9
cts 35
cp 0.2571
rs 5.2941
cc 9
eloc 25
nc 16
nop 1
crap 42.2105
1
<?php
2
3
namespace Majora\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Compiler pass to register actions into related factories
12
 */
13
class ActionRegisterCompilerPass implements CompilerPassInterface
14
{
15 2
    public function process(ContainerBuilder $container)
16
    {
17
        // retrieve factories
18 2
        $factoriesMap = new ArrayCollection();
19 2
        $factoriesTags = $container->findTaggedServiceIds('majora.domain.action_factory');
20 2
        foreach ($factoriesTags as $serviceId => $tags) {
21
            foreach ($tags as $attributes) {
22
                if (empty($attributes['namespace'])) {
23
                    throw new \InvalidArgumentException(sprintf('
24
                        "majora.domain.action_factory" tags into "%s" service has to provide "namespace" attribute.',
25
                        $serviceId
26
                    ));
27
                }
28
                $factoriesMap->set(            // only one factory in each namespaces
29
                    $attributes['namespace'],
30
                    $container->getDefinition($serviceId)
31
                );
32
            }
33 1
        }
34
35
        // retrieve actions
36 2
        $actionsTags = $container->findTaggedServiceIds('majora.domain.action');
37 2
        foreach ($actionsTags as $serviceId => $tags) {
38
            foreach ($tags as $attributes) {
39
                if (empty($attributes['alias']) || empty($attributes['namespace'])) {
40
                    throw new \InvalidArgumentException(sprintf('
41
                        "majora.domain.action" tags into "%s" service has to provide "alias" and "namespace" attributes.',
42
                        $serviceId
43
                    ));
44
                }
45
                if (!$factoriesMap->containsKey($attributes['namespace'])) {
46
                    throw new \InvalidArgumentException(sprintf('
47
                        Any action factory defined for "%s" namespace, defined into "%s" service.',
48
                        $attributes['namespace'],
49
                        $serviceId
50
                    ));
51
                }
52
53
                $factoriesMap->get($attributes['namespace'])
54
                    ->addMethodCall('registerAction', array(
55
                        $attributes['alias'],
56
                        new Reference($serviceId)
57
                    ))
58
                ;
59
            }
60 1
        }
61 2
    }
62
}
63