ConnectionRegistry::__construct()   A
last analyzed

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 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thumper;
4
5
use PhpAmqpLib\Connection\AbstractConnection;
6
7
class ConnectionRegistry
8
{
9
    /**
10
     * @var AbstractConnection[]
11
     */
12
    protected $connections;
13
14
    /**
15
     * @var string
16
     */
17
    protected $defaultConnection;
18
19
    /**
20
     * ConnectionRegistry constructor.
21
     *
22
     * @param AbstractConnection[] $connections Array of connections.
23
     * @param string $defaultConnection Key of default connection.
24
     */
25 15
    public function __construct(array $connections, $defaultConnection)
26
    {
27 15
        $this->connections = $connections;
28 15
        $this->defaultConnection = $defaultConnection;
29 15
    }
30
31
    /**
32
     * Return a connection.
33
     *
34
     * @param string $name Key of connection to get.
35
     * @return AbstractConnection
36
     */
37 15
    public function getConnection($name = null)
38
    {
39 15
        if (null === $name) {
40 5
            $name = $this->defaultConnection;
41 4
        }
42
43 15
        if (!isset($this->connections[$name])) {
44 5
            throw new \InvalidArgumentException(sprintf('AMQP Connection named "%s" does not exist.', $name));
45
        }
46
47 10
        return $this->connections[$name];
48
    }
49
50
    /**
51
     * Add connection to connections registry.
52
     *
53
     * @param string $name Key for connection.
54
     * @param AbstractConnection $connection Connection object.
55
     */
56 5
    public function addConnection($name, AbstractConnection $connection)
57
    {
58 5
        $this->connections[$name] = $connection;
59 5
    }
60
}
61