Completed
Push — master ( 8eb315...5aaba6 )
by Changwan
04:29
created

DatabaseManager::connection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
namespace Wandu\Database;
3
4
class DatabaseManager
5
{
6
    /** @var \Wandu\Database\Connector[] */
7
    protected $connectors = [];
8
    
9
    /** @var \Wandu\Database\Contracts\Connection[] */
10
    protected $connections = [];
11
    
12
    /** @var \Wandu\Database\Repository[] */
13
    protected $repositories = [];
14
15 16
    public function __construct(Configuration $config)
16
    {
17 16
        $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18 16
    }
19
    
20
    /**
21
     * @param array|\Wandu\Database\Connector $connector
22
     * @param string $name
23
     * @return \Wandu\Database\Contracts\Connection
24
     */
25 16
    public function connect($connector, $name = 'default')
26
    {
27 16
        if (is_array($connector)) {
28 16
            $connector = new Connector($connector);
29
        }
30 16
        $this->connectors[$name] = $connector;
31 16
        $connection = $connector->connect();
32 15
        if ($emitter = $this->config->getEmitter()) {
33 14
            $connection->setEventEmitter($emitter);
34
        }
35 15
        return $this->connections[$name] = $connection;
36
    }
37
38
    /**
39
     * @param string $name
40
     * @return \Wandu\Database\Contracts\Connection
41
     */
42
    public function connection($name = 'default')
43
    {
44
        return isset($this->connections[$name]) ? $this->connections[$name] : null;
45
    }
46
47
    /**
48
     * @param string $class
49
     * @return \Wandu\Database\Repository
50
     */
51 12
    public function repository(string $class): Repository
52
    {
53 12
        if (!isset($this->repositories[$class])) {
54
55 12
            $meta = $this->config->getMetadataReader()->getMetadata($class);
56 12
            $connection = $meta->getConnection();
57 12
            $prefix = $this->connectors[$connection]->getPrefix();
58
59 12
            $this->repositories[$class] = new Repository(
60 12
                $this->connections[$connection],
61 12
                new QueryBuilder($prefix . $meta->getTable()),
62
                $meta,
63 12
                $this->config
64
            );
65
        }
66 12
        return $this->repositories[$class];
67
    }
68
}
69