Passed
Push — master ( b0f8c9...462be0 )
by Sébastien
03:24
created

ConnectionDriverFactory   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 41.94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 170
ccs 26
cts 62
cp 0.4194
rs 10
wmc 20

8 Methods

Rating   Name   Duplication   Size   Complexity  
A connectionNames() 0 3 1
A __construct() 0 6 1
A create() 0 9 2
A defaultConnection() 0 3 1
A defaultConnectionName() 0 3 1
A registerConfigurator() 0 4 2
A createDriver() 0 7 2
B configureKnownDriver() 0 49 10
1
<?php
2
3
namespace Bdf\QueueBundle\ConnectionFactory;
4
5
use Bdf\Queue\Connection\AmqpLib\AmqpLibConnection;
6
use Bdf\Queue\Connection\ConnectionDriverInterface;
7
use Bdf\Queue\Connection\Doctrine\DoctrineConnection;
8
use Bdf\Queue\Connection\Factory\ConnectionDriverFactoryInterface;
9
use Bdf\Queue\Connection\Gearman\GearmanConnection;
10
use Bdf\Queue\Connection\Memory\MemoryConnection;
11
use Bdf\Queue\Connection\Null\NullConnection;
12
use Bdf\Queue\Connection\Pheanstalk\PheanstalkConnection;
13
use Bdf\Queue\Connection\RdKafka\RdKafkaConnection;
14
use Bdf\Queue\Connection\Redis\RedisConnection;
15
use Bdf\Queue\Serializer\SerializerInterface;
16
use InvalidArgumentException;
17
use Psr\Container\ContainerInterface;
18
19
/**
20
 *
21
 */
22
final class ConnectionDriverFactory implements ConnectionDriverFactoryInterface
23
{
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * @var string
31
     */
32
    private $containerId;
33
34
    /**
35
     * @var string
36
     */
37
    private $defaultConnection;
38
39
    /**
40
     * @var string[]
41
     */
42
    private $connectionNames;
43
44
    /**
45
     * @var ConnectionDriverConfiguratorInterface[]
46
     */
47
    private $configurators = [];
48
49
50
    /**
51
     * @param ContainerInterface $container
52
     * @param string|null $defaultConnection The default connection name
53
     * @param string[] $connectionNames All the connection names
54
     * @param string $containerId
55
     */
56 4
    public function __construct(ContainerInterface $container, string $defaultConnection = null, array $connectionNames = [], string $containerId = 'bdf_queue.connection_definition.%s')
57
    {
58 4
        $this->container = $container;
59 4
        $this->containerId = $containerId;
60 4
        $this->defaultConnection = $defaultConnection;
61 4
        $this->connectionNames = $connectionNames;
62 4
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    public function create(?string $name): ConnectionDriverInterface
68
    {
69 2
        $id = sprintf($this->containerId, $name);
70
71 2
        if (!$this->container->has($id)) {
72
            throw new InvalidArgumentException('No queue driver has been set for '.$name);
73
        }
74
75 2
        return $this->container->get($id);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function defaultConnectionName(): string
82
    {
83
        return $this->defaultConnection;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function defaultConnection(): ConnectionDriverInterface
90
    {
91
        return $this->create($this->defaultConnection);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function connectionNames(): array
98
    {
99
        return $this->connectionNames;
100
    }
101
102
    /**
103
     * Register a custom configurator
104
     *
105
     * @param ConnectionDriverConfiguratorInterface $configurator
106
     */
107 4
    public function registerConfigurator(ConnectionDriverConfiguratorInterface $configurator): void
108
    {
109 4
        foreach ($configurator->getSupportedDrivers() as $driver) {
110 4
            $this->configurators[$driver] = $configurator;
111
        }
112 4
    }
113
114
    /**
115
     * Create the connection driver instance
116
     *
117
     * @param Configuration $config
118
     * @param SerializerInterface $serializer
119
     *
120
     * @return ConnectionDriverInterface
121
     *
122
     * @internal
123
     */
124 2
    public function createDriver(Configuration $config, SerializerInterface $serializer)
125
    {
126 2
        if (isset($this->configurators[$config->getDriver()])) {
127 1
            return $this->configurators[$config->getDriver()]->configure($config, $serializer);
128
        }
129
130 1
        return $this->configureKnownDriver($config, $serializer);
131
    }
132
133
    /**
134
     * Create the known driver instance
135
     *
136
     * @param Configuration $config
137
     * @param SerializerInterface $serializer
138
     *
139
     * @return ConnectionDriverInterface
140
     *
141
     * @internal
142
     */
143 1
    private function configureKnownDriver(Configuration $config, SerializerInterface $serializer)
144
    {
145 1
        switch ($config->getDriver()) {
146 1
            case 'null':
147
                $connection = new NullConnection($config->getConnection());
148
                $connection->setConfig($config->toArray());
149
                return $connection;
150
151 1
            case 'memory':
152
                $connection = new MemoryConnection($config->getConnection(), $serializer);
153
                $connection->setConfig($config->toArray());
154
                return $connection;
155
156 1
            case 'gearman':
157 1
                $connection = new GearmanConnection($config->getConnection(), $serializer);
158 1
                $connection->setConfig($config->toArray());
159 1
                return $connection;
160
161
            case 'amqp-lib':
162
                $connection = new AmqpLibConnection(
163
                    $config->getConnection(),
164
                    $serializer,
165
                    $config->has('exchange_resolver') ? $this->container->get($config->get('exchange_resolver')) : null
0 ignored issues
show
Bug introduced by
It seems like $config->get('exchange_resolver') can also be of type null; however, parameter $id of Psr\Container\ContainerInterface::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
                    $config->has('exchange_resolver') ? $this->container->get(/** @scrutinizer ignore-type */ $config->get('exchange_resolver')) : null
Loading history...
166
                );
167
                $connection->setConfig($config->toArray());
168
                return $connection;
169
170
            case 'pheanstalk':
171
                $connection = new PheanstalkConnection($config->getConnection(), $serializer);
172
                $connection->setConfig($config->toArray());
173
                return $connection;
174
175
            case 'rdkafka':
176
                $connection = new RdKafkaConnection($config->getConnection(), $serializer);
177
                $connection->setConfig($config->toArray());
178
                return $connection;
179
180
            case 'redis':
181
                $connection = new RedisConnection($config->getConnection(), $serializer);
182
                $connection->setConfig($config->toArray());
183
                return $connection;
184
185
            case 'doctrine':
186
                $connection = new DoctrineConnection($config->getConnection(), $serializer);
187
                $connection->setConfig($config->toArray());
188
                return $connection;
189
        }
190
191
        throw new InvalidArgumentException('The queue driver "'.$config->getDriver().'" does not exist. Did you forget to add "connection_factory" option ?');
192
    }
193
}
194