Passed
Pull Request — master (#3)
by Sébastien
03:50 queued 12s
created

ConnectionDriverFactory::defaultConnectionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 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 string $name  The name of the driver
106
     * @param ConnectionDriverConfiguratorInterface $configurator
107
     */
108 4
    public function registerConfigurator(ConnectionDriverConfiguratorInterface $configurator): void
109
    {
110 4
        foreach ($configurator->getSupportedDrivers() as $driver) {
111 4
            $this->configurators[$driver] = $configurator;
112
        }
113 4
    }
114
115
    /**
116
     * Create the connection driver instance
117
     *
118
     * @param Configuration $config
119
     * @param SerializerInterface $serializer
120
     *
121
     * @return ConnectionDriverInterface
122
     *
123
     * @internal
124
     */
125 2
    public function createDriver(Configuration $config, SerializerInterface $serializer)
126
    {
127 2
        if (isset($this->configurators[$config->getDriver()])) {
128 1
            return $this->configurators[$config->getDriver()]->configure($config, $serializer);
129
        }
130
131 1
        return $this->configureKnownDriver($config, $serializer);
132
    }
133
134
    /**
135
     * Create the known driver instance
136
     *
137
     * @param Configuration $config
138
     * @param SerializerInterface $serializer
139
     *
140
     * @return ConnectionDriverInterface
141
     *
142
     * @internal
143
     */
144 1
    private function configureKnownDriver(Configuration $config, SerializerInterface $serializer)
145
    {
146 1
        switch ($config->getDriver()) {
147 1
            case 'null':
148
                $connection = new NullConnection($config->getConnection());
149
                $connection->setConfig($config->toArray());
150
                return $connection;
151
152 1
            case 'memory':
153
                $connection = new MemoryConnection($config->getConnection(), $serializer);
154
                $connection->setConfig($config->toArray());
155
                return $connection;
156
157 1
            case 'gearman':
158 1
                $connection = new GearmanConnection($config->getConnection(), $serializer);
159 1
                $connection->setConfig($config->toArray());
160 1
                return $connection;
161
162
            case 'amqp-lib':
163
                $connection = new AmqpLibConnection(
164
                    $config->getConnection(),
165
                    $serializer,
166
                    $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

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