Completed
Push — master ( d0f8f2...ef5419 )
by Pierre
07:45 queued 05:26
created

Factory::connect()   A

Complexity

Conditions 3
Paths 10

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 15
ccs 9
cts 12
cp 0.75
crap 3.1406
rs 9.9
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 6
    public function __construct(Container $container)
36
    {
37 6
        $this->container = $container;
38 6
        $configService = $this->container->getService(\App\Config::class);
39 6
        $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 1
    public function getConnection(string $slot, string $dbname): PDO
50
    {
51 1
        $id = $this->identity($slot, $dbname);
52 1
        if (isset($this->connectionPool[$id])) {
53 1
            return $this->connectionPool[$id];
54
        }
55 1
        $this->connect($slot, $dbname);
56 1
        return $this->connectionPool[$id];
57
    }
58
59
    /**
60
     * connect
61
     *
62
     * @param string $slot
63
     * @param string $dbname
64
     * @return void
65
     */
66 1
    protected function connect(string $slot, string $dbname): Factory
67
    {
68 1
        $this->connectionPool = (is_null($this->connectionPool))
0 ignored issues
show
introduced by
The condition is_null($this->connectionPool) is always false.
Loading history...
69 1
            ? []
70
            : $this->connectionPool;
71 1
        $params = $this->adapterParams($slot, $dbname);
72
        try {
73 1
            $adapter = new $params[self::_ADAPTER]($dbname, $params);
74 1
            $adapter->connect();
75 1
            $id = $this->identity($slot, $dbname);
76 1
            $this->connectionPool[$id] = $adapter->getConnection();
77
        } catch (\Exception $e) {
78
            throw new Exception($e);
79
        }
80 1
        return $this;
81
    }
82
83
    /**
84
     * adapter params
85
     *
86
     * @param string $slot
87
     * @param string $dbname
88
     * @return array
89
     */
90 2
    protected function adapterParams(string $slot, string $dbname): array
91
    {
92 2
        if (false === isset($this->config[$slot][$dbname])) {
93 1
            $exMsg = sprintf(
94 1
                'Missing or invalid config on slot %s for db %s',
95 1
                $slot,
96 1
                $dbname
97
            );
98 1
            throw new \Exception($exMsg);
99
        }
100 1
        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 1
    protected function identity(string $slot, string $dbname): string
111
    {
112 1
        return sprintf('%s-%s', $slot, $dbname);
113
    }
114
}
115