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

DefaultConnectionFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
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