|
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\Application; |
|
12
|
|
|
|
|
13
|
|
|
use InMemoryList\Application\Exceptions\MalformedParametersException; |
|
14
|
|
|
use InMemoryList\Application\Exceptions\NotSupportedDriverException; |
|
15
|
|
|
use InMemoryList\Domain\Model\Contracts\ListRepositoryInterface; |
|
16
|
|
|
use InMemoryList\Domain\Model\ListElement; |
|
17
|
|
|
use InMemoryList\Domain\Model\ListElementUuid; |
|
18
|
|
|
use InMemoryList\Infrastructure\Domain\Model\ListCollectionFactory; |
|
19
|
|
|
|
|
20
|
|
|
class Client |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $driver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ListRepositoryInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $repository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Client constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $driver |
|
36
|
|
|
* @param array $parameters |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($driver = 'redis', array $parameters = [], $createSchema = false) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$this->setDriver($driver); |
|
41
|
|
|
$this->setRepository($driver, $parameters, $createSchema = false); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $driver |
|
46
|
|
|
* |
|
47
|
|
|
* @throws NotSupportedDriverException |
|
48
|
|
|
*/ |
|
49
|
|
|
private function setDriver($driver) |
|
50
|
|
|
{ |
|
51
|
|
|
$allowedDrivers = [ |
|
52
|
|
|
'apcu', |
|
53
|
|
|
'memcached', |
|
54
|
|
|
'pdo', |
|
55
|
|
|
'redis', |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
if (!in_array($driver, $allowedDrivers)) { |
|
59
|
|
|
throw new NotSupportedDriverException($driver.' is not a supported driver.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->driver = $driver; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getDriver() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->driver; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $driver |
|
75
|
|
|
* @param array $config |
|
76
|
|
|
*/ |
|
77
|
|
|
private function setRepository($driver, array $config = []) |
|
78
|
|
|
{ |
|
79
|
|
|
$repository = 'InMemoryList\Infrastructure\Persistance\\'.ucfirst($driver).'Repository'; |
|
80
|
|
|
$driver = 'InMemoryList\Infrastructure\Drivers\\'.ucfirst($driver).'Driver'; |
|
81
|
|
|
$instance = (new $driver($config))->getInstance(); |
|
82
|
|
|
|
|
83
|
|
|
$this->repository = new $repository($instance); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return ListRepositoryInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getRepository() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->repository; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $elements |
|
96
|
|
|
* @param array $parameters |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed|string |
|
99
|
|
|
* |
|
100
|
|
|
* @throws MalformedParametersException |
|
101
|
|
|
*/ |
|
102
|
|
|
public function create(array $elements, array $parameters = []) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->validateParameters($parameters); |
|
105
|
|
|
$factory = new ListCollectionFactory(); |
|
106
|
|
|
$list = $factory->create( |
|
107
|
|
|
$elements, |
|
108
|
|
|
(isset($parameters['headers'])) ? $parameters['headers'] : [], |
|
109
|
|
|
(isset($parameters['uuid'])) ? $parameters['uuid'] : null, |
|
110
|
|
|
(isset($parameters['element-uuid'])) ? $parameters['element-uuid'] : null |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
return $this->repository->create( |
|
114
|
|
|
$list, |
|
115
|
|
|
(isset($parameters['ttl'])) ? $parameters['ttl'] : null, |
|
116
|
|
|
(isset($parameters['chunk-size'])) ? $parameters['chunk-size'] : null |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param $parameters |
|
122
|
|
|
* |
|
123
|
|
|
* @throws MalformedParametersException |
|
124
|
|
|
*/ |
|
125
|
|
|
private function validateParameters($parameters) |
|
126
|
|
|
{ |
|
127
|
|
|
$allowedParameters = [ |
|
128
|
|
|
'chunk-size', |
|
129
|
|
|
'element-uuid', |
|
130
|
|
|
'headers', |
|
131
|
|
|
'ttl', |
|
132
|
|
|
'uuid', |
|
133
|
|
|
]; |
|
134
|
|
|
|
|
135
|
|
|
foreach (array_keys($parameters) as $key) { |
|
136
|
|
|
if (!in_array($key, $allowedParameters)) { |
|
137
|
|
|
throw new MalformedParametersException(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param $listUuid |
|
144
|
|
|
* @param $elementUuid |
|
145
|
|
|
* @param array $data |
|
146
|
|
|
* |
|
147
|
|
|
* @return mixed |
|
148
|
|
|
*/ |
|
149
|
|
|
public function pushElement($listUuid, $elementUuid, array $data = []) |
|
150
|
|
|
{ |
|
151
|
|
|
$newElement = new ListElement( |
|
152
|
|
|
new ListElementUuid($elementUuid), |
|
153
|
|
|
$data |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
return $this->repository->pushElement($listUuid, $newElement); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.