Completed
Pull Request — master (#3)
by Rémy
01:26
created

Rorm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author Rémy M. Böhler <[email protected]>
4
 */
5
declare(strict_types=1);
6
7
namespace Rorm;
8
9
use PDO;
10
11
class Rorm implements ConnectionResolver
12
{
13
    private $defaultConnectionName = 'default';
14
15
    /** @var PDO[] */
16
    protected $connections = [];
17
18 6
    public function __construct(\PDO $defaultConnection)
19
    {
20 6
        $this->setConnection($this->defaultConnectionName, $defaultConnection);
21 6
    }
22
23
    public function register()
24
    {
25
        Model::setConnectionResolver($this);
26
    }
27
28 6
    public function setConnection(string $name, PDO $connection): void
29
    {
30 6
        $this->connections[$name] = $connection;
31 6
    }
32
33 6
    public function connection(string $name): \PDO
34
    {
35 6
        if (array_key_exists($name, $this->connections)) {
36 4
            return $this->connections[$name];
37
        }
38
39 2
        throw new ConnectionNotFoundException('Database connection not found!');
40
    }
41
42 2
    public function defaultConnection(): \PDO
43
    {
44 2
        return $this->connection($this->defaultConnectionName);
45
    }
46
}
47