Passed
Push — master ( add0cc...b0f8c9 )
by Sébastien
10:03
created

ConnectionDriverFactory::createDriver()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 58.1521

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 37
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 50
ccs 8
cts 37
cp 0.2162
crap 58.1521
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
46
     * @param ContainerInterface $container
47
     * @param string|null $defaultConnection The default connection name
48
     * @param string[] $connectionNames All the connection names
49
     * @param string $containerId
50
     */
51 3
    public function __construct(ContainerInterface $container, string $defaultConnection = null, array $connectionNames = [], string $containerId = 'bdf_queue.connection_definition.%s')
52
    {
53 3
        $this->container = $container;
54 3
        $this->containerId = $containerId;
55 3
        $this->defaultConnection = $defaultConnection;
56 3
        $this->connectionNames = $connectionNames;
57 3
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function create(?string $name): ConnectionDriverInterface
63
    {
64 1
        $id = sprintf($this->containerId, $name);
65
66 1
        if (!$this->container->has($id)) {
67
            throw new InvalidArgumentException('No queue driver has been set for '.$name);
68
        }
69
70 1
        return $this->container->get($id);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function defaultConnectionName(): string
77
    {
78
        return $this->defaultConnection;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function defaultConnection(): ConnectionDriverInterface
85
    {
86
        return $this->create($this->defaultConnection);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function connectionNames(): array
93
    {
94
        return $this->connectionNames;
95
    }
96
97
    /**
98
     * Create the connection driver instance
99
     *
100
     * @param Configuration $config
101
     * @param SerializerInterface $serializer
102
     *
103
     * @return ConnectionDriverInterface
104
     */
105 1
    public function createDriver(Configuration $config, SerializerInterface $serializer)
106
    {
107 1
        switch ($config->getDriver()) {
108 1
            case 'null':
109
                $connection = new NullConnection($config->getConnection());
110
                $connection->setConfig($config->toArray());
111
                return $connection;
112
113 1
            case 'memory':
114
                $connection = new MemoryConnection($config->getConnection(), $serializer);
115
                $connection->setConfig($config->toArray());
116
                return $connection;
117
118 1
            case 'gearman':
119 1
                $connection = new GearmanConnection($config->getConnection(), $serializer);
120 1
                $connection->setConfig($config->toArray());
121 1
                return $connection;
122
123
            case 'amqp-lib':
124
                $connection = new AmqpLibConnection(
125
                    $config->getConnection(),
126
                    $serializer,
127
                    $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

127
                    $config->has('exchange_resolver') ? $this->container->get(/** @scrutinizer ignore-type */ $config->get('exchange_resolver')) : null
Loading history...
128
                );
129
                $connection->setConfig($config->toArray());
130
                return $connection;
131
132
            case 'pheanstalk':
133
                $connection = new PheanstalkConnection($config->getConnection(), $serializer);
134
                $connection->setConfig($config->toArray());
135
                return $connection;
136
137
            case 'rdkafka':
138
                $connection = new RdKafkaConnection($config->getConnection(), $serializer);
139
                $connection->setConfig($config->toArray());
140
                return $connection;
141
142
            case 'redis':
143
                $connection = new RedisConnection($config->getConnection(), $serializer);
144
                $connection->setConfig($config->toArray());
145
                return $connection;
146
147
            case 'doctrine':
148
                $connection = new DoctrineConnection($config->getConnection(), $serializer);
149
                $connection->setConfig($config->toArray());
150
                return $connection;
151
152
        }
153
154
        throw new InvalidArgumentException('The queue driver "'.$config->getDriver().'" does not exist. Did you forget to add "connection_factory" option ?');
155
    }
156
}
157