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

Rorm::connection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 2
eloc 4
nc 2
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 $defaultConnectionName = 'default';
14
15
    /** @var PDO[] */
16
    protected $connections = [];
17
18
    public function __construct(\PDO $defaultConnection)
19
    {
20
        $this->setConnection($this->defaultConnectionName, $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($this->defaultConnectionName);
45
    }
46
}
47