Completed
Pull Request — master (#3)
by Rémy
08:33
created

Rorm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 const DEFAULT_CONNECTION_NAME = 'default';
14
15
    /** @var PDO[] */
16
    protected $connections = [];
17
18
    public function __construct(\PDO $defaultConnection)
19
    {
20
        $this->setConnection(self::DEFAULT_CONNECTION_NAME, $defaultConnection);
21
    }
22
23
    public function register()
24
    {
25 1
        Model::setConnectionResolver($this);
26
    }
27 1
28 1
    public function setConnection(string $name, PDO $connection): void
29
    {
30
        $this->connections[$name] = $connection;
31
    }
32
33
    public function connection(string $name): \PDO
34
    {
35 50
        if (array_key_exists($name, $this->connections)) {
36
            return $this->connections[$name];
37 50
        }
38 49
39
        throw new ConnectionNotFoundException('Database connection not found!');
40
    }
41 1
42
    public function defaultConnection(): \PDO
43
    {
44
        return $this->connection(self::DEFAULT_CONNECTION_NAME);
45
    }
46
}
47