Passed
Push — master ( 30f57b...f3320a )
by huang
02:57
created

DatabasePool   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getConnection() 0 23 7
A initPool() 0 6 2
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Pool;
11
12
use FastD\Model\Database;
13
14
/**
15
 * Class DatabasePool.
16
 */
17
class DatabasePool implements PoolInterface
18
{
19
    /**
20
     * @var Database[]
21
     */
22
    protected $connections = [];
23
24
    /**
25
     * @var array
26
     */
27
    protected $config;
28
29
    /**
30
     * Database constructor.
31
     *
32
     * @param array $config
33
     */
34 46
    public function __construct(array $config)
35
    {
36 46
        $this->config = $config;
37 46
    }
38
39
    /**
40
     * @param $key
41
     *
42
     * @return Database
43
     */
44 47
    public function getConnection($key)
45
    {
46 47
        if (!isset($this->connections[$key])) {
47 46
            if (!isset($this->config[$key])) {
48 10
                throw new \LogicException(sprintf('No set %s database', $key));
49
            }
50 36
            $config = $this->config[$key];
51 36
            $this->connections[$key] = new Database(
52
                [
53 36
                    'database_type' => isset($config['adapter']) ? $config['adapter'] : 'mysql',
54 36
                    'database_name' => $config['name'],
55 36
                    'server' => $config['host'],
56 36
                    'username' => $config['user'],
57 36
                    'password' => $config['pass'],
58 36
                    'charset' => isset($config['charset']) ? $config['charset'] : 'utf8',
59 36
                    'port' => isset($config['port']) ? $config['port'] : 3306,
60 36
                    'prefix' => isset($config['prefix']) ? $config['prefix'] : '',
61
                ]
62 36
            );
63 36
        }
64
65 37
        return $this->connections[$key];
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function initPool()
72
    {
73 1
        foreach ($this->config as $name => $config) {
74 1
            $this->getConnection($name);
75 1
        }
76 1
    }
77
}
78