Passed
Push — master ( 83757d...6bbb4f )
by Vincent
07:14
created

ConnectionDriverFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
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
    /**
41
     * @param ContainerInterface $container
42
     * @param string $defaultConnection The default connection name
43
     * @param string $containerId
44
     */
45 2
    public function __construct(ContainerInterface $container, string $defaultConnection = null, string $containerId = 'bdf_queue.connection_definition.%s')
46
    {
47 2
        $this->container = $container;
48 2
        $this->containerId = $containerId;
49 2
        $this->defaultConnection = $defaultConnection;
50 2
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function create(?string $name): ConnectionDriverInterface
56
    {
57 1
        $id = sprintf($this->containerId, $name);
58
59 1
        if (!$this->container->has($id)) {
60
            throw new InvalidArgumentException('No queue driver has been set for '.$name);
61
        }
62
63 1
        return $this->container->get($id);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function defaultConnectionName(): string
70
    {
71
        return $this->defaultConnection;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function defaultConnection(): ConnectionDriverInterface
78
    {
79
        return $this->create($this->defaultConnection);
80
    }
81
82
    /**
83
     * Create the connection driver instance
84
     *
85
     * @param Configuration $config
86
     * @param SerializerInterface $serializer
87
     *
88
     * @return ConnectionDriverInterface
89
     */
90 1
    public function createDriver(Configuration $config, SerializerInterface $serializer)
91
    {
92 1
        switch ($config->getDriver()) {
93 1
            case 'null':
94
                $connection = new NullConnection($config->getConnection());
95
                $connection->setConfig($config->toArray());
96
                return $connection;
97
98 1
            case 'memory':
99
                $connection = new MemoryConnection($config->getConnection(), $serializer);
100
                $connection->setConfig($config->toArray());
101
                return $connection;
102
103 1
            case 'gearman':
104 1
                $connection = new GearmanConnection($config->getConnection(), $serializer);
105 1
                $connection->setConfig($config->toArray());
106 1
                return $connection;
107
108
            case 'amqp-lib':
109
                $connection = new AmqpLibConnection(
110
                    $config->getConnection(),
111
                    $serializer,
112
                    $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

112
                    $config->has('exchange_resolver') ? $this->container->get(/** @scrutinizer ignore-type */ $config->get('exchange_resolver')) : null
Loading history...
113
                );
114
                $connection->setConfig($config->toArray());
115
                return $connection;
116
117
            case 'pheanstalk':
118
                $connection = new PheanstalkConnection($config->getConnection(), $serializer);
119
                $connection->setConfig($config->toArray());
120
                return $connection;
121
122
            case 'rdkafka':
123
                $connection = new RdKafkaConnection($config->getConnection(), $serializer);
124
                $connection->setConfig($config->toArray());
125
                return $connection;
126
127
            case 'redis':
128
                $connection = new RedisConnection($config->getConnection(), $serializer);
129
                $connection->setConfig($config->toArray());
130
                return $connection;
131
132
            case 'doctrine':
133
                $connection = new DoctrineConnection($config->getConnection(), $serializer);
134
                $connection->setConfig($config->toArray());
135
                return $connection;
136
137
        }
138
139
        throw new InvalidArgumentException('The queue driver "'.$config->getDriver().'" does not exist. Did you forget to add "connection_factory" option ?');
140
    }
141
}
142