Completed
Push — develop ( eba14b...6e538a )
by Baptiste
02:17
created

InnmindAMQPExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 15
cts 15
cp 1
rs 9.4285
cc 1
eloc 15
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\AMQPBundle\DependencyInjection;
5
6
use Innmind\AMQPBundle\Producer\Producer;
7
use Symfony\Component\{
8
    HttpKernel\DependencyInjection\Extension,
9
    DependencyInjection\ContainerBuilder,
10
    DependencyInjection\Loader,
11
    DependencyInjection\Reference,
12
    DependencyInjection\Definition,
13
    Config\FileLocator
14
};
15
16
final class InnmindAMQPExtension extends Extension
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 6
    public function load(array $configs, ContainerBuilder $container)
22
    {
23 6
        $loader = new Loader\YamlFileLoader(
24 6
            $container,
25 6
            new FileLocator(__DIR__.'/../Resources/config')
26
        );
27 6
        $loader->load('services.yml');
28 6
        $config = $this->processConfiguration(
29 6
            new Configuration,
30 6
            $configs
31
        );
32
33
        $this
34 6
            ->configureConnection($config, $container)
35 6
            ->registerTranslators($config, $container)
36 6
            ->registerExchanges($config, $container)
37 6
            ->registerQueues($config, $container)
38 6
            ->registerBindings($config, $container)
39 6
            ->registerProducers($config, $container);
40 6
    }
41
42 6
    private function configureConnection(
43
        array $config,
44
        ContainerBuilder $container
45
    ): self {
46
        $container
47 6
            ->getDefinition('innmind.amqp.connection.default')
48 6
            ->replaceArgument(1, $config['server'])
49 6
            ->replaceArgument(3, $config['server']['timeout'])
50 6
            ->replaceArgument(4, new Reference($config['clock']));
51
52 6
        return $this;
53
    }
54
55 6
    private function registerTranslators(
56
        array $config,
57
        ContainerBuilder $container
58
    ): self {
59 6
        $definition = $container->getDefinition('innmind.amqp.argument_translator');
60
61 6
        foreach ($config['argument_translators'] as $translator) {
62
            $definition->addArgument(new Reference($translator));
63
        }
64
65 6
        return $this;
66
    }
67
68 6 View Code Duplication
    private function registerExchanges(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
        array $config,
70
        ContainerBuilder $container
71
    ): self {
72 6
        $autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare');
73
74 6
        foreach ($config['exchanges'] as $name => $exchange) {
75 4
            $autoDeclare->addMethodCall(
76 4
                'declareExchange',
77 4
                [$name, $exchange['type'], $exchange['durable'], $exchange['arguments']]
78
            );
79
        }
80
81 6
        return $this;
82
    }
83
84 6
    private function registerQueues(
85
        array $config,
86
        ContainerBuilder $container
87
    ): self {
88 6
        $autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare');
89 6
        $consumers = $container->getDefinition('innmind.amqp.consumers');
90
91 6
        foreach ($config['queues'] as $name => $queue) {
92 5
            $autoDeclare->addMethodCall(
93 5
                'declareQueue',
94 5
                [$name, $queue['durable'], $queue['exclusive'], $queue['arguments']]
95
            );
96 5
            $consumers->addMethodCall(
97 5
                'add',
98 5
                [$name, new Reference($queue['consumer'])]
99
            );
100
        }
101
102 6
        return $this;
103
    }
104
105 6 View Code Duplication
    private function registerBindings(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
        array $config,
107
        ContainerBuilder $container
108
    ): self {
109 6
        $autoDeclare = $container->getDefinition('innmind.amqp.client.auto_declare');
110
111 6
        foreach ($config['bindings'] as $binding) {
112 4
            $autoDeclare->addMethodCall(
113 4
                'declareBinding',
114 4
                [$binding['exchange'], $binding['queue'], $binding['routingKey'], $binding['arguments']]
115
            );
116
        }
117
118 6
        return $this;
119
    }
120
121 6
    private function registerProducers(
122
        array $config,
123
        ContainerBuilder $container
124
    ): self {
125 6
        foreach ($config['exchanges'] as $name => $_) {
126 4
            $container->setDefinition(
127 4
                'innmind.amqp.producer.'.$name,
128 4
                new Definition(
129 4
                    Producer::class,
130
                    [
131 4
                        new Reference('innmind.amqp.client'),
132 4
                        $name
133
                    ]
134
                )
135
            );
136
        }
137
138 6
        return $this;
139
    }
140
}
141