Passed
Push — master ( 7248ab...0a52e0 )
by Pierre
10:18
created

Factory::identity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Component\Db;
4
5
use \PDO;
6
use Nymfonya\Component\Config;
7
use Nymfonya\Component\Container;
8
use App\Component\Db\Pool;
9
use Monolog\Logger;
10
11
class Factory
12
{
13
    const _ADAPTER = 'adapter';
14
    const LOGGER_PREFIX = 'Db Factory Pool : ';
15
16
    private $container;
17
18
    /**
19
     * factory config
20
     *
21
     * @var array
22
     */
23
    private $config;
24
25
    /**
26
     * connection pool
27
     *
28
     * @var Pool
29
     */
30
    private $pool;
31
32
    /**
33
     * logger
34
     *
35
     * @var Logger
36
     */
37
    private $logger;
38
39
    /**
40
     * instanciate factory
41
     *
42
     * @param Container $container
43
     */
44 8
    public function __construct(Container $container)
45
    {
46 8
        $this->container = $container;
47 8
        $configService = $this->container->getService(Config::class);
48 8
        $this->config = $configService->getSettings(Config::_DB);
49 8
        $this->logger = $this->container->getService(Logger::class);
50 8
        $this->pool = $this->container->getService(Pool::class);
51 8
        $this->logger->info(self::LOGGER_PREFIX . 'instance ');
52
    }
53
54
    /**
55
     * return Pdo connection instance from pool
56
     *
57
     * @param string $slot
58
     * @param string $dbname
59
     * @return PDO
60
     */
61 1
    public function getConnection(string $slot, string $dbname): PDO
62
    {
63 1
        $id = $this->identity($slot, $dbname);
64 1
        if (isset($this->pool[$id])) {
65 1
            return $this->pool[$id];
66
        }
67 1
        $this->connect($slot, $dbname);
68 1
        return $this->pool[$id];
69
    }
70
71
    /**
72
     * connect
73
     *
74
     * @param string $slot
75
     * @param string $dbname
76
     * @return void
77
     */
78 2
    protected function connect(string $slot, string $dbname): Factory
79
    {
80 2
        $params = $this->adapterParams($slot, $dbname);
81
        try {
82 2
            $adapter = new $params[self::_ADAPTER]($dbname, $params);
83 2
            $adapter->connect();
84 1
            $id = $this->identity($slot, $dbname);
85 1
            $this->pool[$id] = $adapter->getConnection();
86 1
        } catch (\PDOException $e) {
87 1
            $exMsg = self::LOGGER_PREFIX .  ' connection failed';
88 1
            $this->logger->warn($exMsg);
89 1
            throw new \Exception($exMsg, $e->getCode());
90
        }
91 1
        return $this;
92
    }
93
94
    /**
95
     * adapter params
96
     *
97
     * @param string $slot
98
     * @param string $dbname
99
     * @return array
100
     */
101 2
    protected function adapterParams(string $slot, string $dbname): array
102
    {
103 2
        if (false === isset($this->config[$slot][$dbname])) {
104 1
            $exMsg = sprintf(
105 1
                'Missing or invalid config on slot %s for db %s',
106 1
                $slot,
107 1
                $dbname
108
            );
109 1
            $this->logger->warn($exMsg);
110 1
            throw new \Exception($exMsg);
111
        }
112 1
        return $this->config[$slot][$dbname];
113
    }
114
115
    /**
116
     * identify pool index
117
     *
118
     * @param string $slot
119
     * @param string $dbname
120
     * @return string
121
     */
122 1
    protected function identity(string $slot, string $dbname): string
123
    {
124 1
        return sprintf('%s-%s', $slot, $dbname);
125
    }
126
127
    /**
128
     * return pool service
129
     *
130
     * @return Pool
131
     */
132 2
    protected function getPool(): Pool
133
    {
134 2
        return $this->pool;
135
    }
136
}
137