ConfigureSenderCompilerPass   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 3
dl 0
loc 80
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 11 3
A registerChannels() 0 28 5
A registerMessages() 0 25 5
1
<?php
2
3
namespace Yokai\MessengerBundle\DependencyInjection\CompilerPass;
4
5
use InvalidArgumentException;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Yokai\MessengerBundle\Channel\ChannelInterface;
11
use Yokai\MessengerBundle\Message;
12
13
/**
14
 * @author Yann Eugoné <[email protected]>
15
 */
16
class ConfigureSenderCompilerPass implements CompilerPassInterface
17
{
18
    /**
19
     * @inheritdoc
20
     */
21 15
    public function process(ContainerBuilder $container)
22
    {
23 15
        if (!$container->hasDefinition('yokai_messenger.sender') && !$container->hasAlias('yokai_messenger.sender')) {
24 1
            return;
25
        }
26
27 14
        $definition = $container->findDefinition('yokai_messenger.sender');
28
29 14
        $this->registerChannels($definition, $container);
30 12
        $this->registerMessages($definition, $container);
31 10
    }
32
33
    /**
34
     * @param Definition       $definition
35
     * @param ContainerBuilder $container
36
     */
37 14
    private function registerChannels(Definition $definition, ContainerBuilder $container)
38
    {
39 14
        foreach ($container->findTaggedServiceIds('yokai_messenger.channel') as $id => $config) {
40 10
            if (!is_a($container->getDefinition($id)->getClass(), ChannelInterface::class, true)) {
41 1
                throw new InvalidArgumentException(
42 1
                    sprintf('Service "%s" must implement interface "%s".', $id, ChannelInterface::class)
43
                );
44
            }
45
46 9
            if (!isset($config[0]['alias'])) {
47 1
                throw new InvalidArgumentException(
48 1
                    sprintf(
49 1
                        'Service "%s" must define the "alias" attribute on "messenger.channel" tags.',
50 1
                        $id
51
                    )
52
                );
53
            }
54
55 8
            $definition->addMethodCall(
56 8
                'addChannel',
57
                [
58 8
                    new Reference($id),
59 8
                    $config[0]['alias'],
60 8
                    isset($config[0]['priority']) ? $config[0]['priority'] : 1,
61
                ]
62
            );
63
        }
64 12
    }
65
66
    /**
67
     * @param Definition       $definition
68
     * @param ContainerBuilder $container
69
     */
70 12
    private function registerMessages(Definition $definition, ContainerBuilder $container)
71
    {
72 12
        foreach ($container->findTaggedServiceIds('yokai_messenger.message') as $id => $config) {
73 5
            if (Message::class !== $container->getDefinition($id)->getClass()) {
74 1
                throw new InvalidArgumentException(
75 1
                    sprintf('Service "%s" must be a "%s".', $id, Message::class)
76
                );
77
            }
78
79 4
            $messageReference = new Reference($id);
80
81 4
            foreach ($config as $attributes) {
82 4
                if (!isset($attributes['channel'])) {
83 1
                    throw new InvalidArgumentException(
84 1
                        sprintf(
85 1
                            'Service "%s" must define the "channel" attribute on "messenger.message" tags.',
86 1
                            $id
87
                        )
88
                    );
89
                }
90
91 3
                $definition->addMethodCall('addMessage', [$messageReference, $attributes['channel']]);
92
            }
93
        }
94 10
    }
95
}
96