Completed
Push — master ( 0fb3d7...53ff75 )
by Markus
03:02
created

DefaultConnectionFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 33
ccs 0
cts 19
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A addConnection() 0 4 1
A getConnection() 0 10 4
1
<?php
2
namespace Mathielen\ImportEngine\Storage\Provider\Connection;
3
4
use Mathielen\ImportEngine\ValueObject\StorageSelection;
5
6
class DefaultConnectionFactory implements ConnectionFactoryInterface
7
{
8
9
    /**
10
     * @var array
11
     */
12
    private $connections;
13
14
    public function __construct(array $connections)
15
    {
16
        $this->connections = $connections;
17
18
        if (!isset($connections['default'])) {
19
            throw new \InvalidArgumentException("At least a 'default' connection must be given");
20
        }
21
    }
22
23
    public function addConnection($id, $connection)
24
    {
25
        $this->connections[$id] = $connection;
26
    }
27
28
    public function getConnection(StorageSelection $selection = null)
29
    {
30
        $id = $selection ? $selection->getId() : null;
31
32
        if (!$id || !isset($this->connections[$id])) {
33
            $id = 'default';
34
        }
35
36
        return $this->connections[$id];
37
    }
38
}
39