Completed
Push — master ( 5652bd...9532f5 )
by Pierre
02:48
created

Factory::identity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Component\Db;
4
5
use \PDO;
6
use App\Component\Config;
7
use App\Component\Container;
8
use Exception;
9
10
class Factory
11
{
12
    const _ADAPTER = 'adapter';
13
14
    private $container;
15
16
    /**
17
     * factory config
18
     *
19
     * @var array
20
     */
21
    private $config;
22
23
    /**
24
     * connection pool
25
     *
26
     * @var array
27
     */
28
    private $connectionPool;
29
30
    /**
31
     * instanciate factory
32
     *
33
     * @param Container $container
34
     */
35
    public function __construct(Container $container)
36
    {
37
        $this->container = $container;
38
        $configService = $this->container->getService(\App\Config::class);
39
        $this->config = $configService->getSettings(Config::_DB);
40
    }
41
42
    /**
43
     * return Pdo connection instance from pool
44
     *
45
     * @param string $slot
46
     * @param string $dbname
47
     * @return PDO
48
     */
49
    public function getConnection(string $slot, string $dbname): PDO
50
    {
51
        $id = $this->identity($slot, $dbname);
52
        if (isset($this->connectionPool[$id])) {
53
            return $this->connectionPool[$id];
54
        }
55
        $this->connect($slot, $dbname);
56
        return $this->connectionPool[$id];
57
    }
58
59
    /**
60
     * connect
61
     *
62
     * @param string $slot
63
     * @param string $dbname
64
     * @return void
65
     */
66
    protected function connect(string $slot, string $dbname): Factory
67
    {
68
        $this->connectionPool = (is_null($this->connectionPool))
0 ignored issues
show
introduced by
The condition is_null($this->connectionPool) is always false.
Loading history...
69
            ? []
70
            : $this->connectionPool;
71
        $params = $this->adapterParams($slot, $dbname);
72
        try {
73
            $adapter = new $params[self::_ADAPTER]($dbname, $params);
74
            $adapter->connect();
75
            $id = $this->identity($slot, $dbname);
76
            $this->connectionPool[$id] = $adapter->getConnection();
77
        } catch (\Exception $e) {
78
            throw new Exception($e);
79
        }
80
        return $this;
81
    }
82
83
    /**
84
     * adapter params
85
     *
86
     * @param string $slot
87
     * @param string $dbname
88
     * @return array
89
     */
90
    protected function adapterParams(string $slot, string $dbname)
91
    {
92
        if (false === isset($this->config[$slot][$dbname])) {
93
            $exMsg = sprintf(
94
                'Missing or invalid config on slot %s for db %s',
95
                $slot,
96
                $dbname
97
            );
98
            throw new \Exception($exMsg);
99
        }
100
        return $this->config[$slot][$dbname];
101
    }
102
103
    /**
104
     * identify pool index
105
     *
106
     * @param string $slot
107
     * @param string $dbname
108
     * @return string
109
     */
110
    protected function identity(string $slot, string $dbname): string
111
    {
112
        return sprintf('%s-%s', $slot, $dbname);
113
    }
114
}
115