DefaultConnectionFactory::getConnection()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Mathielen\ImportEngine\Storage\Provider\Connection;
4
5
use Mathielen\ImportEngine\ValueObject\StorageSelection;
6
7
class DefaultConnectionFactory implements ConnectionFactoryInterface
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