ConnectionRegistry   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 5
c 2
b 0
f 2
lcom 1
cbo 0
dl 0
loc 54
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConnection() 0 12 3
A addConnection() 0 4 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