Completed
Pull Request — master (#10)
by
unknown
02:30
created

processCommandServices()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.2
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace Clearcode\CommandBusConsole\Bundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
class CommandHandlersCompilerPass implements CompilerPassInterface
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public function process(ContainerBuilder $container)
14
    {
15
        $this->processCommandServices($container);
16
        $this->processFormTypes($container);
17
    }
18
19
    /**
20
     * @param ContainerBuilder $container
21
     */
22
    private function processCommandServices(ContainerBuilder $container)
23
    {
24
        if (!$container->hasDefinition('command_bus_console.command_collector')) {
25
            return;
26
        }
27
28
        $commandHandlersTags = $container->findTaggedServiceIds('command_handler');
29
        $commandCollector = $container->getDefinition('command_bus_console.command_collector');
30
31
        $commands = [];
32
33
        foreach ($commandHandlersTags as $service) {
34
            foreach ($service as $tags) {
35
                $commands[] = $tags['handles'];
36
            }
37
        }
38
39
        $commandCollector->addMethodCall('processCommandServices', [$commands]);
40
    }
41
42
    /**
43
     * @param ContainerBuilder $container
44
     * @throws CommandFormTypeDuplicate
45
     * @throws CommandFormTypeMissingFormTypeTag
46
     */
47
    private function processFormTypes(ContainerBuilder $container)
48
    {
49
        if (!$container->hasDefinition('command_bus_console.command_form_type_map')) {
50
            return;
51
        }
52
53
        $commandFormTypesTags = $container->findTaggedServiceIds('command_form_type');
54
        $commandFormTypesCollector = $container->getDefinition('command_bus_console.command_form_type_map');
55
56
        $commandFormTypes = [];
57
58
        foreach ($commandFormTypesTags as $serviceName => $service) {
59
            $allTags = $container->getDefinition($serviceName)->getTags();
60
61
            if (!array_key_exists('form.type', $allTags)) {
62
                throw new CommandFormTypeMissingFormTypeTag();
63
            }
64
65
            foreach ($allTags['command_form_type'] as $commandFormTypeTag) {
66
                if (array_key_exists($commandFormTypeTag['command_class'], $commandFormTypes)) {
67
                    throw new CommandFormTypeDuplicate();
68
                }
69
70
                $commandFormTypes[$commandFormTypeTag['command_class']] = $serviceName;
71
            }
72
        }
73
74
        $commandFormTypesCollector->addMethodCall('processFormTypeServices', [$commandFormTypes]);
75
    }
76
}
77