Passed
Push — master ( 848a13...c93e13 )
by Mauro
02:28
created

PdoDriver::setConfig()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 13
Ratio 46.43 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 3
nop 1
dl 13
loc 28
rs 6.7272
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 View Code Duplication
        foreach ($config as $param => $server) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    /**
102
     * @return bool
103
     */
104
    public function connect()
105
    {
106
        $servers = $this->config ?: [];
107
108
        if (count($servers) === 1) {
109
            $servers = $servers[0];
110
        }
111
112
        if (count($servers) === 0) {
113
            $servers = [
114
                'host' => '127.0.0.1',
115
                'port' => 3306,
116
                'username' => 'root',
117
                'password' => null,
118
                'driver' => 'mysql',
119
                'charset' => 'utf8',
120
                'database' => 'in-memory-list',
121
            ];
122
        }
123
124
        $port = (isset($servers['port'])) ? $servers['port'] : '3306';
125
        $charset = (isset($servers['charset'])) ? $servers['charset'] : 'utf8';
126
127
        $dsn = $servers['driver'].':host='.$servers['host'].':'.$port.';dbname='.$servers['database'].';charset='.$charset;
128
129
        $this->instance = new \PDO($dsn, $servers['username'], $servers['password']);
130
131
        return true;
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137
    public function getInstance()
138
    {
139
        return $this->instance;
140
    }
141
}
142