1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SplObjectStorage; |
7
|
|
|
|
8
|
|
|
class Repository |
9
|
|
|
{ |
10
|
|
|
private $space; |
11
|
|
|
private $persisted = []; |
12
|
|
|
private $original = []; |
13
|
|
|
private $keys; |
14
|
|
|
|
15
|
|
|
private $cache = []; |
16
|
|
|
private $results = []; |
17
|
|
|
|
18
|
|
|
public function __construct(Space $space) |
19
|
|
|
{ |
20
|
|
|
$this->space = $space; |
21
|
|
|
$this->keys = new SplObjectStorage; |
22
|
|
|
} |
23
|
64 |
|
|
24
|
|
|
public function create($data) |
25
|
64 |
|
{ |
26
|
64 |
|
$class = Entity::class; |
27
|
|
View Code Duplication |
foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
|
|
|
28
|
64 |
|
$entityClass = $plugin->getEntityClass($this->space); |
29
|
|
|
if($entityClass) { |
30
|
64 |
|
if($class != Entity::class) { |
31
|
64 |
|
throw new Exception('Entity class override'); |
32
|
|
|
} |
33
|
|
|
$class = $entityClass; |
34
|
64 |
|
} |
35
|
2 |
|
} |
36
|
|
|
$instance = new $class(); |
37
|
|
|
foreach($this->space->getFormat() as $row) { |
38
|
64 |
|
if(array_key_exists($row['name'], $data)) { |
39
|
64 |
|
$instance->{$row['name']} = $data[$row['name']]; |
40
|
64 |
|
} |
41
|
64 |
|
} |
42
|
4 |
|
|
43
|
4 |
|
foreach($this->space->getMapper()->getPlugins() as $plugin) { |
44
|
|
|
$plugin->beforeCreate($instance, $this->space); |
45
|
64 |
|
} |
46
|
64 |
|
|
47
|
64 |
|
// validate instance key |
48
|
64 |
|
$key = $this->space->getInstanceKey($instance); |
49
|
|
|
|
50
|
|
|
$this->keys[$instance] = $key; |
51
|
64 |
|
return $instance; |
52
|
64 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function findOne($params = []) |
55
|
|
|
{ |
56
|
|
|
return $this->find($params, true); |
57
|
|
|
} |
58
|
64 |
|
|
59
|
2 |
|
public function find($params = [], $one = false) |
60
|
2 |
|
{ |
61
|
|
|
$cacheIndex = array_search([$params, $one], $this->cache); |
62
|
64 |
|
if($cacheIndex !== false) { |
63
|
|
|
return $this->results[$cacheIndex]; |
64
|
64 |
|
} |
65
|
64 |
|
|
66
|
64 |
|
if(!is_array($params)) { |
67
|
64 |
|
$params = [$params]; |
68
|
64 |
|
} |
69
|
|
|
if(count($params) == 1 && array_key_exists(0, $params)) { |
70
|
|
|
$primary = $this->space->getPrimaryIndex(); |
71
|
|
|
if(count($primary->parts) == 1) { |
72
|
64 |
|
$formatted = $this->space->getMapper()->getSchema()->formatValue($primary->parts[0][1], $params[0]); |
73
|
|
|
if($params[0] == $formatted) { |
74
|
64 |
|
$params = [ |
75
|
|
|
$this->space->getFormat()[$primary->parts[0][0]]['name'] => $params[0] |
76
|
|
|
]; |
77
|
64 |
|
} |
78
|
|
|
} |
79
|
64 |
|
} |
80
|
|
|
|
81
|
64 |
|
if(array_key_exists('id', $params)) { |
82
|
|
|
if(array_key_exists($params['id'], $this->persisted)) { |
83
|
|
|
$instance = $this->persisted[$params['id']]; |
84
|
64 |
|
return $one ? $instance : [$instance]; |
85
|
|
|
} |
86
|
64 |
|
} |
87
|
64 |
|
|
88
|
64 |
|
|
89
|
64 |
|
$index = $this->space->castIndex($params); |
90
|
|
|
if(is_null($index)) { |
91
|
64 |
|
throw new Exception("No index for params ".json_encode($params)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$cacheIndex = count($this->cache); |
95
|
1 |
|
$this->cache[] = [$params, $one]; |
96
|
|
|
|
97
|
|
|
$client = $this->space->getMapper()->getClient(); |
98
|
19 |
|
$values = $this->space->getIndexValues($index, $params); |
99
|
|
|
|
100
|
19 |
|
$data = $client->getSpace($this->space->getId())->select($values, $index)->getData(); |
101
|
|
|
|
102
|
|
|
$result = []; |
103
|
64 |
|
foreach($data as $tuple) { |
|
|
|
|
104
|
|
|
$instance = $this->getInstance($tuple); |
105
|
64 |
|
if($one) { |
106
|
|
|
return $this->results[$cacheIndex] = $instance; |
107
|
64 |
|
} |
108
|
1 |
|
$result[] = $instance; |
109
|
|
|
} |
110
|
|
|
|
111
|
64 |
|
if($one) { |
112
|
10 |
|
return $this->results[$cacheIndex] = null; |
113
|
3 |
|
} |
114
|
|
|
|
115
|
|
|
return $this->results[$cacheIndex] = $result; |
116
|
7 |
|
} |
117
|
|
|
|
118
|
7 |
|
private function getInstance($tuple) |
119
|
|
|
{ |
120
|
|
|
$key = $this->space->getTupleKey($tuple); |
121
|
64 |
|
|
122
|
2 |
|
if(array_key_exists($key, $this->persisted)) { |
123
|
|
|
return $this->persisted[$key]; |
124
|
|
|
} |
125
|
64 |
|
|
126
|
64 |
|
$class = Entity::class; |
127
|
|
View Code Duplication |
foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
|
|
|
128
|
64 |
|
$entityClass = $plugin->getEntityClass($this->space); |
129
|
64 |
|
if($entityClass) { |
130
|
18 |
|
if($class != Entity::class) { |
131
|
64 |
|
throw new Exception('Entity class override'); |
132
|
|
|
} |
133
|
|
|
$class = $entityClass; |
134
|
|
|
} |
135
|
64 |
|
} |
136
|
1 |
|
$instance = new $class(); |
137
|
|
|
|
138
|
|
|
$this->original[$key] = $tuple; |
139
|
64 |
|
|
140
|
64 |
View Code Duplication |
foreach($this->space->getFormat() as $index => $info) { |
|
|
|
|
141
|
2 |
|
$instance->{$info['name']} = array_key_exists($index, $tuple) ? $tuple[$index] : null; |
142
|
2 |
|
} |
143
|
|
|
|
144
|
64 |
|
$this->keys->offsetSet($instance, $key); |
145
|
64 |
|
|
146
|
|
|
return $this->persisted[$key] = $instance; |
147
|
1 |
|
} |
148
|
64 |
|
|
149
|
|
|
public function knows($instance) |
150
|
|
|
{ |
151
|
|
|
return $this->keys->offsetExists($instance); |
152
|
|
|
} |
153
|
64 |
|
|
154
|
64 |
|
public function update(Entity $instance, $operations) |
155
|
64 |
|
{ |
156
|
|
|
if(!count($operations)) { |
157
|
|
|
return; |
158
|
64 |
|
} |
159
|
64 |
|
|
160
|
2 |
|
$tupleOperations = []; |
161
|
|
|
foreach($operations as $operation) { |
162
|
|
|
$tupleIndex = $this->space->getPropertyIndex($operation[1]); |
163
|
64 |
|
$tupleOperations[] = [$operation[0], $tupleIndex, $operation[2]]; |
164
|
|
|
} |
165
|
64 |
|
|
166
|
1 |
|
$pk = []; |
167
|
1 |
|
foreach($this->space->getPrimaryIndex()->parts as $part) { |
168
|
1 |
|
$pk[] = $instance->{$this->space->getFormat()[$part[0]]['name']}; |
169
|
1 |
|
} |
170
|
1 |
|
|
171
|
|
|
$client = $this->space->getMapper()->getClient(); |
172
|
|
|
$result = $client->getSpace($this->space->getId())->update($pk, $tupleOperations); |
173
|
1 |
|
foreach($result->getData() as $tuple) { |
174
|
|
View Code Duplication |
foreach($this->space->getFormat() as $index => $info) { |
|
|
|
|
175
|
|
|
if(array_key_exists($index, $tuple)) { |
176
|
1 |
|
$instance->{$info['name']} = $tuple[$index]; |
177
|
|
|
} |
178
|
1 |
|
} |
179
|
1 |
|
} |
180
|
|
|
} |
181
|
1 |
|
|
182
|
|
|
public function remove($instance) |
183
|
|
|
{ |
184
|
64 |
|
$key = $this->space->getInstanceKey($instance); |
185
|
|
|
|
186
|
64 |
|
if(!array_key_exists($key, $this->original)) { |
187
|
64 |
|
return; |
188
|
64 |
|
} |
189
|
64 |
|
|
190
|
64 |
|
if(array_key_exists($key, $this->persisted)) { |
191
|
64 |
|
|
192
|
64 |
|
unset($this->persisted[$key]); |
193
|
|
|
|
194
|
64 |
|
$pk = []; |
195
|
|
View Code Duplication |
foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
|
|
|
196
|
64 |
|
$pk[] = $this->original[$key][$part[0]]; |
197
|
64 |
|
} |
198
|
|
|
|
199
|
14 |
|
$this->space->getMapper()->getClient() |
200
|
|
|
->getSpace($this->space->getId()) |
201
|
|
|
->delete($pk); |
202
|
64 |
|
|
203
|
64 |
|
} |
204
|
|
|
|
205
|
17 |
|
unset($this->original[$key]); |
206
|
|
|
|
207
|
|
|
$this->results = []; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function save($instance) |
211
|
|
|
{ |
212
|
64 |
|
$tuple = []; |
213
|
|
|
|
214
|
64 |
|
$size = count(get_object_vars($instance)); |
215
|
|
|
|
216
|
|
|
foreach($this->space->getFormat() as $index => $info) { |
217
|
6 |
|
if(!property_exists($instance, $info['name'])) { |
218
|
|
|
$instance->{$info['name']} = null; |
219
|
6 |
|
} |
220
|
6 |
|
|
221
|
6 |
|
$instance->{$info['name']} = $this->space->getMapper()->getSchema() |
222
|
|
|
->formatValue($info['type'], $instance->{$info['name']}); |
223
|
6 |
|
$tuple[$index] = $instance->{$info['name']}; |
224
|
6 |
|
|
225
|
|
|
if(count($tuple) == $size) { |
226
|
1 |
|
break; |
227
|
|
|
} |
228
|
1 |
|
} |
229
|
1 |
|
|
230
|
1 |
|
$key = $this->space->getInstanceKey($instance); |
231
|
|
|
$client = $this->space->getMapper()->getClient(); |
232
|
1 |
|
|
233
|
1 |
|
if(array_key_exists($key, $this->persisted)) { |
234
|
|
|
// update |
235
|
64 |
|
$update = array_diff_assoc($tuple, $this->original[$key]); |
236
|
|
|
if(!count($update)) { |
237
|
64 |
|
return $instance; |
238
|
64 |
|
} |
239
|
|
|
|
240
|
64 |
|
$operations = []; |
241
|
|
|
foreach($update as $index => $value) { |
242
|
64 |
|
$operations[] = ['=', $index, $value]; |
243
|
1 |
|
} |
244
|
|
|
|
245
|
|
|
$pk = []; |
246
|
64 |
View Code Duplication |
foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
|
|
|
247
|
64 |
|
$pk[] = $this->original[$key][$part[0]]; |
248
|
64 |
|
} |
249
|
64 |
|
|
250
|
|
|
$client->getSpace($this->space->getId())->update($pk, $operations); |
251
|
19 |
|
$this->original[$key] = $tuple; |
252
|
19 |
|
|
253
|
19 |
|
} else { |
254
|
19 |
|
$client->getSpace($this->space->getId())->insert($tuple); |
255
|
|
|
$this->persisted[$key] = $instance; |
256
|
|
|
$this->original[$key] = $tuple; |
257
|
19 |
|
} |
258
|
19 |
|
|
259
|
4 |
|
|
260
|
19 |
|
$this->flushCache(); |
261
|
19 |
|
} |
262
|
|
|
|
263
|
|
|
public function flushCache() |
264
|
|
|
{ |
265
|
|
|
$this->cache = []; |
266
|
|
|
} |
267
|
|
|
} |
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.