PdoDriver::setConfig()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 6
nop 1
dl 0
loc 28
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the InMemoryList package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 */
10
11
namespace InMemoryList\Infrastructure\Drivers;
12
13
use InMemoryList\Infrastructure\Drivers\Contracts\DriverInterface;
14
use InMemoryList\Infrastructure\Drivers\Exceptions\PdoDriverCheckException;
15
use InMemoryList\Infrastructure\Drivers\Exceptions\PdoMalformedConfigException;
16
17
class PdoDriver implements DriverInterface
18
{
19
    /**
20
     * @var
21
     */
22
    private $config;
23
24
    /**
25
     * @var \PDO
26
     */
27
    private $instance;
28
29
    /**
30
     * RedisDriver constructor.
31
     *
32
     * @codeCoverageIgnore
33
     *
34
     * @param array $config
35
     *
36
     * @throws PdoDriverCheckException
37
     */
38
    public function __construct(array $config = [])
39
    {
40
        $this->setConfig($config);
41
        if (!$this->check()) {
42
            throw new PdoDriverCheckException('PDO extension is not loaded.');
43
        }
44
45
        $this->connect();
46
    }
47
48
    /**
49
     * @param $config
50
     *
51
     * @throws PdoMalformedConfigException
52
     */
53
    private function setConfig($config)
54
    {
55
        $allowedConfigKeys = [
56
            'driver',
57
            'host',
58
            'username',
59
            'password',
60
            'database',
61
            'port',
62
            'options',
63
            'charset',
64
        ];
65
66
        foreach ($config as $param => $server) {
67
            if (is_array($server)) {
68
                foreach (array_keys($server) as $key) {
69
                    if (!in_array($key, $allowedConfigKeys)) {
70
                        throw new PdoMalformedConfigException();
71
                    }
72
                }
73
            }
74
75
            if (!is_array($server) && !in_array($param, $allowedConfigKeys)) {
76
                throw new PdoMalformedConfigException();
77
            }
78
        }
79
80
        $this->config = $config;
81
    }
82
83
    /**
84
     * @codeCoverageIgnore
85
     *
86
     * @return bool
87
     */
88
    public function check()
89
    {
90
        return extension_loaded('PDO');
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public function clear()
97
    {
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function connect()
104
    {
105
        $servers = $this->config ?: [];
106
107
        if (count($servers) === 1) {
108
            $servers = $servers[0];
109
        }
110
111
        $default = [
112
            'host' => '127.0.0.1',
113
            'port' => 3306,
114
            'username' => 'root',
115
            'password' => null,
116
            'driver' => 'mysql',
117
            'charset' => 'utf8',
118
            'database' => 'in-memory-list',
119
        ];
120
121
        $servers = array_merge($default, $servers);
122
        $dsn = sprintf(
123
            '%s:host=%s;port=%s;dbname=%s;charset=%s',
124
            $servers['driver'],
125
            $servers['host'],
126
            $servers['port'] ?? '3306',
127
            $servers['database'],
128
            $servers['charset'] ?? 'utf8'
129
            );
130
131
        $this->instance = new \PDO($dsn, $servers['username'], $servers['password']);
132
133
        return true;
134
    }
135
136
    /**
137
     * @return mixed
138
     */
139
    public function getInstance()
140
    {
141
        return $this->instance;
142
    }
143
}
144