Passed
Pull Request — master (#2)
by Vincent
03:17
created

BdfQueueExtension::configureConnections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 22
ccs 15
cts 15
cp 1
crap 2
rs 9.7998
1
<?php
2
3
namespace Bdf\QueueBundle\DependencyInjection;
4
5
use Bdf\Queue\Connection\ConnectionDriverInterface;
6
use Bdf\Queue\Failer\DbFailedJobStorage;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Failer\DbFailedJobStorage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Bdf\QueueBundle\ConnectionFactory\Configuration as DriverConfiguration;
8
use Bdf\QueueBundle\ConnectionFactory\ConnectionDriverFactory;
9
use Bdf\QueueBundle\DependencyInjection\Failer\RegisterFailerDriverPass;
10
use Bdf\QueueBundle\DependencyInjection\Failer\FailerDriverConfiguratorInterface;
11
use Bdf\QueueBundle\FailerFactory\FailerFactory;
0 ignored issues
show
Bug introduced by
The type Bdf\QueueBundle\FailerFactory\FailerFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Bdf\QueueBundle\FailerFactory\PrimeFailerFactory;
0 ignored issues
show
Bug introduced by
The type Bdf\QueueBundle\FailerFactory\PrimeFailerFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Config\FileLocator;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Extension\Extension;
16
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * QueueExtension
21
 */
22
class BdfQueueExtension extends Extension
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27 5
    public function load(array $configs, ContainerBuilder $container)
28
    {
29 5
        $configuration = $this->getConfiguration($configs, $container);
30 5
        $config = $this->processConfiguration($configuration, $configs);
31
32 5
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
33 5
        $loader->load('queue.yaml');
34
35 5
        $this->configureConnections($config, $container);
36 5
        $this->configureDestinations($config, $container);
37 5
        $this->configureFailer($config, $container);
38 5
    }
39
40
    /**
41
     * @param array $config
42
     * @param ContainerBuilder $container
43
     */
44 5
    public function configureConnections(array $config, ContainerBuilder $container): void
45
    {
46 5
        $connectionConfigs = [];
47
48 5
        foreach ($config['connections'] as $name => $options) {
49 5
            $connectionConfigs[$name] = $options;
50
51 5
            $container->register('bdf_queue.config_definition.'.$name, DriverConfiguration::class)
52 5
                ->setFactory([DriverConfiguration::class, 'createConfiguration'])
53 5
                ->setArguments([$name, $options]);
54
55 5
            $container->register('bdf_queue.connection_definition.'.$name, ConnectionDriverInterface::class)
56 5
                ->setPublic(true)
57 5
                ->setFactory($options['connection_factory'] ?? [new Reference(ConnectionDriverFactory::class), 'createDriver'])
58 5
                ->setArguments([
59 5
                    new Reference('bdf_queue.config_definition.'.$name),
60 5
                    new Reference($this->configureSerializer($options['serializer'], $config['default_serializer']))
61
                ]);
62
        }
63
64 5
        $container->setParameter('bdf_queue.default_connection', $config['default_connection'] ?? key($connectionConfigs));
65 5
        $container->setParameter('bdf_queue.connections', $connectionConfigs);
66 5
    }
67
68
    /**
69
     * @param array $config
70
     * @param string $default
71
     *
72
     * @return string
73
     */
74 5
    private function configureSerializer(array $config, string $default): string
75
    {
76 5
        if (isset($config['service'])) {
77
            return $config['service'];
78
        }
79
80 5
        return 'bdf_queue.serializer.'.($config['id'] ?? $default);
81
    }
82
83
    /**
84
     * @param array $config
85
     * @param ContainerBuilder $container
86
     */
87 5
    public function configureDestinations(array $config, ContainerBuilder $container): void
88
    {
89 5
        $destinations = [];
90 5
        $consumptionConfig = [];
91
92 5
        foreach ($config['destinations'] as $name => $options) {
93 5
            $destinations[$name] = $options['url'];
94
95
            // TODO build a builder rule in the container for this destination
96 5
            foreach ($options['consumer'] as $option => $value) {
97 5
                if ($value !== null) {
98 5
                    if (($value[0] ?? '') === '@') {
99
                        $value = new Reference(ltrim($value, '@'));
100
                    }
101
102 5
                    $consumptionConfig[$name][$option] = $value;
103
                }
104
            }
105
        }
106
107 5
        $container->getDefinition('bdf_queue.receiver.loader')
108 5
            ->replaceArgument(1, $consumptionConfig);
109
110 5
        $container->setParameter('bdf_queue.destinations', $destinations);
111 5
    }
112
113
    /**
114
     * @param array $config
115
     * @param ContainerBuilder $container
116
     */
117 5
    public function configureFailer(array $config, ContainerBuilder $container): void
118
    {
119 5
        $container->setParameter('bdf_queue.failer_dsn', $config['failer']);
120
121 5
        $container->registerForAutoconfiguration(FailerDriverConfiguratorInterface::class)
122 5
            ->setShared(false)
123 5
            ->setPublic(false)
124 5
            ->addTag(RegisterFailerDriverPass::CONFIGURATOR_TAG_NAME)
125
        ;
126 5
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131 5
    public function getConfiguration(array $config, ContainerBuilder $container): Configuration
132
    {
133 5
        return new Configuration($container->getParameter('kernel.debug'));
134
    }
135
}
136