Passed
Push — master ( 0efcf5...db0e0f )
by Vincent
08:47
created

ConnectionDriverFactory   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Test Coverage

Coverage 37.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 167
ccs 23
cts 62
cp 0.371
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 registerConfigurator() 0 4 2
A defaultConnectionName() 0 3 1
A createDriver() 0 7 2
B configureKnownDriver() 0 57 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
final class ConnectionDriverFactory implements ConnectionDriverFactoryInterface
20
{
21
    /**
22
     * @var ContainerInterface
23
     */
24
    private $container;
25
26
    /**
27
     * @var string
28
     */
29
    private $containerId;
30
31
    /**
32
     * @var string
33
     */
34
    private $defaultConnection;
35
36
    /**
37
     * @var string[]
38
     */
39
    private $connectionNames;
40
41
    /**
42
     * @var ConnectionDriverConfiguratorInterface[]
43
     */
44
    private $configurators = [];
45
46
    /**
47
     * @param string|null $defaultConnection The default connection name
48
     * @param string[]    $connectionNames   All the connection names
49
     */
50 5
    public function __construct(ContainerInterface $container, string $defaultConnection = null, array $connectionNames = [], string $containerId = 'bdf_queue.connection_definition.%s')
51
    {
52 5
        $this->container = $container;
53 5
        $this->containerId = $containerId;
54 5
        $this->defaultConnection = $defaultConnection;
55 5
        $this->connectionNames = $connectionNames;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 3
    public function create(?string $name): ConnectionDriverInterface
62
    {
63 3
        $id = sprintf($this->containerId, $name);
64
65 3
        if (!$this->container->has($id)) {
66
            throw new InvalidArgumentException('No queue driver has been set for '.$name);
67
        }
68
69 3
        return $this->container->get($id);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function defaultConnectionName(): string
76
    {
77
        return $this->defaultConnection;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function defaultConnection(): ConnectionDriverInterface
84
    {
85
        return $this->create($this->defaultConnection);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function connectionNames(): array
92
    {
93
        return $this->connectionNames;
94
    }
95
96
    /**
97
     * Register a custom configurator.
98
     */
99 5
    public function registerConfigurator(ConnectionDriverConfiguratorInterface $configurator): void
100
    {
101 5
        foreach ($configurator->getSupportedDrivers() as $driver) {
102 5
            $this->configurators[$driver] = $configurator;
103
        }
104
    }
105
106
    /**
107
     * Create the connection driver instance.
108
     *
109
     * @return ConnectionDriverInterface
110
     *
111
     * @internal
112
     */
113 3
    public function createDriver(Configuration $config, SerializerInterface $serializer)
114
    {
115 3
        if (isset($this->configurators[$config->getDriver()])) {
116 1
            return $this->configurators[$config->getDriver()]->configure($config, $serializer);
117
        }
118
119 2
        return $this->configureKnownDriver($config, $serializer);
120
    }
121
122
    /**
123
     * Create the known driver instance.
124
     *
125
     * @return ConnectionDriverInterface
126
     *
127
     * @internal
128
     */
129 2
    private function configureKnownDriver(Configuration $config, SerializerInterface $serializer)
130
    {
131 2
        switch ($config->getDriver()) {
132 2
            case 'null':
133
                $connection = new NullConnection($config->getConnection());
134
                $connection->setConfig($config->toArray());
135
136
                return $connection;
137
138 2
            case 'memory':
139 2
                $connection = new MemoryConnection($config->getConnection(), $serializer);
140 2
                $connection->setConfig($config->toArray());
141
142 2
                return $connection;
143
144
            case 'gearman':
145
                $connection = new GearmanConnection($config->getConnection(), $serializer);
146
                $connection->setConfig($config->toArray());
147
148
                return $connection;
149
150
            case 'amqp-lib':
151
                $connection = new AmqpLibConnection(
152
                    $config->getConnection(),
153
                    $serializer,
154
                    $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

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