|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the EventStoreManager 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 SimpleEventStoreManager\Infrastructure\Drivers; |
|
12
|
|
|
|
|
13
|
|
|
use SimpleEventStoreManager\Infrastructure\Drivers\Contracts\DriverInterface; |
|
14
|
|
|
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\MalformedDriverConfigException; |
|
15
|
|
|
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\NotInstalledDriverCheckException; |
|
16
|
|
|
|
|
17
|
|
|
class PdoDriver implements DriverInterface |
|
18
|
|
|
{ |
|
19
|
|
|
const EVENTSTORE_TABLE_NAME = 'eventstore'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var |
|
23
|
|
|
*/ |
|
24
|
|
|
private $config; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \PDO |
|
28
|
|
|
*/ |
|
29
|
|
|
private $instance; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* PdoDriver constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @codeCoverageIgnore |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $config |
|
37
|
|
|
* |
|
38
|
|
|
* @throws NotInstalledDriverCheckException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(array $config = []) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->setConfig($config); |
|
43
|
|
|
if (!$this->check()) { |
|
44
|
|
|
throw new NotInstalledDriverCheckException('Pdo is not loaded.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->connect(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $config |
|
52
|
|
|
* |
|
53
|
|
|
* @throws MalformedDriverConfigException |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
private function setConfig($config) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$allowedConfigKeys = [ |
|
58
|
|
|
'database', |
|
59
|
|
|
'driver', |
|
60
|
|
|
'host', |
|
61
|
|
|
'options', |
|
62
|
|
|
'password', |
|
63
|
|
|
'port', |
|
64
|
|
|
'username', |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
foreach (array_keys($config) as $key) { |
|
68
|
|
|
if (!in_array($key, $allowedConfigKeys)) { |
|
69
|
|
|
throw new MalformedDriverConfigException('Pdo Driver: malformed config parameters'); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->config = $config; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @codeCoverageIgnore |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function check() |
|
82
|
|
|
{ |
|
83
|
|
|
return class_exists('\PDO'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function connect() |
|
90
|
|
|
{ |
|
91
|
|
|
$dsn = $this->config['driver'].':dbname='.$this->config['database'].';host='.$this->config['host']; |
|
92
|
|
|
$this->instance = new \PDO($dsn, $this->config['username'], $this->config['password']); |
|
93
|
|
|
$this->createSchema(); |
|
94
|
|
|
|
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* create schema. |
|
100
|
|
|
*/ |
|
101
|
|
|
private function createSchema() |
|
102
|
|
|
{ |
|
103
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `".self::EVENTSTORE_TABLE_NAME."` ( |
|
104
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
105
|
|
|
`uuid` char(36) COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:guid)', |
|
106
|
|
|
`version` int(10) unsigned NOT NULL, |
|
107
|
|
|
`payload` varchar(255) DEFAULT NULL, |
|
108
|
|
|
`type` varchar(255) DEFAULT NULL, |
|
109
|
|
|
`body` longtext, |
|
110
|
|
|
`occurred_on` datetime(6), |
|
111
|
|
|
PRIMARY KEY (`id`) |
|
112
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; |
|
113
|
|
|
|
|
114
|
|
|
$this->instance->exec($query); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
public function instance() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->instance; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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.