Test Failed
Push — master ( 2530ac...3f2955 )
by Vincent
10:10 queued 07:25
created

BdfQueueExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 96
ccs 42
cts 44
cp 0.9545
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 10 1
A configureDestinations() 0 24 5
A getConfiguration() 0 3 1
A configureSerializer() 0 7 2
A configureConnections() 0 22 2
1
<?php
2
3
namespace Bdf\QueueBundle\DependencyInjection;
4
5
use Bdf\QueueBundle\ConnectionFactory\ConnectionDriverFactory;
6
use Bdf\QueueBundle\ConnectionFactory\Configuration as DriverConfiguration;
7
use Bdf\Queue\Connection\ConnectionDriverInterface;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Extension\Extension;
11
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12
use Symfony\Component\DependencyInjection\Reference;
13
14
/**
15
 * QueueExtension
16
 */
17
class BdfQueueExtension extends Extension
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22 1
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 1
        $configuration = $this->getConfiguration($configs, $container);
25 1
        $config = $this->processConfiguration($configuration, $configs);
26
27 1
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
28 1
        $loader->load('queue.yaml');
29
30 1
        $this->configureConnections($config, $container);
31 1
        $this->configureDestinations($config, $container);
32 1
    }
33
34
    /**
35
     * @param array $config
36
     * @param ContainerBuilder $container
37
     */
38 1
    public function configureConnections(array $config, ContainerBuilder $container): void
39
    {
40 1
        $connectionConfigs = [];
41
42 1
        foreach ($config['connections'] as $name => $options) {
43 1
            $connectionConfigs[$name] = $options;
44
45 1
            $container->register('bdf_queue.config_definition.'.$name, DriverConfiguration::class)
46 1
                ->setFactory([DriverConfiguration::class, 'createConfiguration'])
47 1
                ->setArguments([$name, $options]);
48
49 1
            $container->register('bdf_queue.connection_definition.'.$name, ConnectionDriverInterface::class)
50 1
                ->setPublic(true)
51 1
                ->setFactory($options['connection_factory'] ?? [new Reference(ConnectionDriverFactory::class), 'createDriver'])
52 1
                ->setArguments([
53 1
                    new Reference('bdf_queue.config_definition.'.$name),
54 1
                    new Reference($this->configureSerializer($options['serializer'], $config['default_serializer']))
55
                ]);
56
        }
57
58 1
        $container->setParameter('bdf_queue.default_connection', $config['default_connection'] ?? key($connectionConfigs));
59 1
        $container->setParameter('bdf_queue.connections', $connectionConfigs);
60 1
    }
61
62
    /**
63
     * @param array $config
64
     * @param string $default
65
     *
66
     * @return string
67
     */
68 1
    private function configureSerializer(array $config, string $default): string
69
    {
70 1
        if (isset($config['service'])) {
71
            return $config['service'];
72
        }
73
74 1
        return 'bdf_queue.serializer.'.($config['id'] ?? $default);
75
    }
76
77
    /**
78
     * @param array $config
79
     * @param ContainerBuilder $container
80
     */
81 1
    public function configureDestinations(array $config, ContainerBuilder $container): void
82
    {
83 1
        $destinations = [];
84 1
        $consumptionConfig = [];
85
86 1
        foreach ($config['destinations'] as $name => $options) {
87 1
            $destinations[$name] = $options['url'];
88
89
            // TODO build a builder rule in the container for this destination
90 1
            foreach ($options['consumer'] as $option => $value) {
91 1
                if ($value !== null) {
92 1
                    if (($value[0] ?? '') === '@') {
93
                        $value = new Reference(ltrim($value, '@'));
94
                    }
95
96 1
                    $consumptionConfig[$name][$option] = $value;
97
                }
98
            }
99
        }
100
101 1
        $container->getDefinition('bdf_queue.receiver.loader')
102 1
            ->replaceArgument(1, $consumptionConfig);
103
104 1
        $container->setParameter('bdf_queue.destinations', $destinations);
105 1
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110 1
    public function getConfiguration(array $config, ContainerBuilder $container): Configuration
111
    {
112 1
        return new Configuration($container->getParameter('kernel.debug'));
113
    }
114
}
115