Completed
Push — master ( 857f68...f292c4 )
by Pierre
02:57
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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