legacyFormTypeAlias()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Clearcode\CommandBusConsole\Bundle\DependencyInjection\Compiler;
4
5
use Clearcode\CommandBusConsole\Bundle\Command\CommandBusHandleCommand;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
10
class RegisterConsoleCommandsCompilerPass implements CompilerPassInterface
11
{
12
    const TAG = 'command_bus.type';
13
14
    /** {@inheritdoc} */
15
    public function process(ContainerBuilder $container)
16
    {
17
        $commandFormTypeTags = $container->findTaggedServiceIds(self::TAG);
18
19
        foreach ($commandFormTypeTags as $formTypeId => $formTypeTagAttributes) {
20
            $formTypeDefinition = $container->getDefinition($formTypeId);
21
22
            $this->assertOnlyOneCommandTypeTagInDefinition($formTypeTagAttributes, $formTypeId);
23
            $this->assertTaggedServiceIsFormType($formTypeDefinition, $formTypeId);
24
            $this->assertTagsRequiredAttribute($formTypeTagAttributes[0], $formTypeId);
25
26
            $container->setDefinition(
27
                sprintf('command_bus_console.%s', str_replace('-', '_', $formTypeTagAttributes[0]['alias'])),
28
                (new Definition())
29
                    ->setClass(CommandBusHandleCommand::class)
30
                    ->addArgument($formTypeTagAttributes[0]['alias'])
31
                    ->addArgument($formTypeDefinition->getClass())
32
                    ->addArgument($this->legacyFormTypeAlias($formTypeDefinition))
33
                    ->addTag('console.command')
34
            );
35
        }
36
    }
37
38
    /**
39
     * @param array  $tagAttributes
40
     * @param string $serviceId
41
     *
42
     * @throws \InvalidArgumentException
43
     */
44
    private function assertOnlyOneCommandTypeTagInDefinition($tagAttributes, $serviceId)
45
    {
46
        if (1 < count($tagAttributes)) {
47
            throw new \InvalidArgumentException(sprintf(
48
                'Service with id "%s" has %d tags named "%s", but only one is allowed.',
49
                $serviceId,
50
                count($tagAttributes),
51
                self::TAG
52
            ));
53
        }
54
    }
55
56
    /**
57
     * @param Definition $formType
58
     * @param string     $serviceId
59
     *
60
     * @throws \InvalidArgumentException
61
     */
62
    public function assertTaggedServiceIsFormType(Definition $formType, $serviceId)
63
    {
64
        if (!$formType->hasTag('form.type')) {
65
            throw new \InvalidArgumentException(sprintf(
66
                'Service with id "%s" has to be tagged with "form.type".',
67
                $serviceId
68
            ));
69
        }
70
    }
71
72
    /**
73
     * @param array  $tagAttributes
74
     * @param string $serviceId
75
     *
76
     * @throws \InvalidArgumentException
77
     */
78
    public function assertTagsRequiredAttribute($tagAttributes, $serviceId)
79
    {
80
        if (!array_key_exists('command', $tagAttributes)) {
81
            throw new \InvalidArgumentException(sprintf(
82
                'Service with id "%s" has tag "%s" with missing attribute "command".',
83
                $serviceId,
84
                self::TAG
85
            ));
86
        }
87
88
        if (!array_key_exists('alias', $tagAttributes)) {
89
            throw new \InvalidArgumentException(sprintf(
90
                'Service with id "%s" has tag "%s" with missing attribute "alias".',
91
                $serviceId,
92
                self::TAG
93
            ));
94
        }
95
    }
96
97
    /**
98
     * @param $formTypeDefinition
99
     *
100
     * @return mixed
101
     */
102
    protected function legacyFormTypeAlias($formTypeDefinition)
103
    {
104
        if (array_key_exists('alias', $formTypeDefinition->getTag('form.type')[0])) {
105
            return $formTypeDefinition->getTag('form.type')[0]['alias'];
106
        }
107
    }
108
}
109