|
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\Persistance; |
|
12
|
|
|
|
|
13
|
|
|
use InMemoryList\Infrastructure\Persistance\Exceptions\ListElementDoesNotExistsException; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractRepository |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param $listUuid |
|
19
|
|
|
* |
|
20
|
|
|
* @return mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
public function delete($listUuid) |
|
23
|
|
|
{ |
|
24
|
|
|
$list = $this->findListByUuid($listUuid); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
foreach (array_keys($list) as $elementUuid) { |
|
27
|
|
|
$this->deleteElement($listUuid, $elementUuid); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $listUuid |
|
33
|
|
|
* @param $elementUuid |
|
34
|
|
|
* |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
public function existsElement($listUuid, $elementUuid) |
|
38
|
|
|
{ |
|
39
|
|
|
return isset($this->findListByUuid($listUuid)[$elementUuid]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $listUuid |
|
44
|
|
|
* @param $elementUuid |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
* |
|
48
|
|
|
* @throws ListElementDoesNotExistsException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function findElement($listUuid, $elementUuid) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$this->existsElement($listUuid, $elementUuid)) { |
|
53
|
|
|
throw new ListElementDoesNotExistsException('Cannot retrieve the element '.$elementUuid.' from the collection in memory.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->findListByUuid($listUuid)[$elementUuid]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $listUuid |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getChunkSize($listUuid) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->existsListInIndex($listUuid)) { |
|
67
|
|
|
$index = $this->getIndex($listUuid); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
return $index['chunk-size']; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $listUuid |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getCounter($listUuid) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->existsListInIndex($listUuid)) { |
|
83
|
|
|
$index = $this->getIndex($listUuid); |
|
84
|
|
|
|
|
85
|
|
|
return $index['size']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $listUuid |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function existsListInIndex($listUuid) |
|
97
|
|
|
{ |
|
98
|
|
|
return ($this->getIndex($listUuid)) ? true : false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $listUuid |
|
103
|
|
|
* |
|
104
|
|
|
* @return float |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getNumberOfChunks($listUuid) |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->existsListInIndex($listUuid)) { |
|
109
|
|
|
$index = $this->getIndex($listUuid); |
|
110
|
|
|
|
|
111
|
|
|
return $index['chunks']; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return 0; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param \Datetime|null $from |
|
119
|
|
|
* @param \Datetime|null $to |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getIndexInRangeDate(\Datetime $from = null, \Datetime $to = null) |
|
124
|
|
|
{ |
|
125
|
|
|
$results = []; |
|
126
|
|
|
|
|
127
|
|
|
if ($index = $this->getIndex()) { |
|
128
|
|
|
foreach ($index as $key => $item) { |
|
129
|
|
|
$unserializedItem = $item; |
|
130
|
|
|
$createdOn = $unserializedItem['created_on']->format('Y-m-d'); |
|
131
|
|
|
$from = $from ? $from->format('Y-m-d') : null; |
|
132
|
|
|
$to = $to ? $to->format('Y-m-d') : null; |
|
133
|
|
|
$firstStatement = ($from) ? $createdOn >= $from : true; |
|
134
|
|
|
$secondStatement = ($to) ? $createdOn <= $to : true; |
|
135
|
|
|
|
|
136
|
|
|
if ($firstStatement && $secondStatement) { |
|
137
|
|
|
$results[$key] = $item; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $results; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param $listUuid |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getTtl($listUuid) |
|
151
|
|
|
{ |
|
152
|
|
|
$index = $this->getIndex($listUuid); |
|
153
|
|
|
if ($index['ttl'] && $index['ttl'] > 0) { |
|
154
|
|
|
$now = new \DateTime('NOW'); |
|
155
|
|
|
$expire_date = $index['created_on']->add(new \DateInterval('PT'.$index['ttl'].'S')); |
|
156
|
|
|
$diffSeconds = $expire_date->getTimestamp() - $now->getTimestamp(); |
|
157
|
|
|
|
|
158
|
|
|
return $diffSeconds; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return -1; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param array $index |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function removeExpiredListsFromIndex($index) |
|
168
|
|
|
{ |
|
169
|
|
|
if (is_array($index)) { |
|
|
|
|
|
|
170
|
|
|
foreach (array_keys($index) as $key) { |
|
171
|
|
|
if (false === $this->exists($key) && isset($index[$key])) { |
|
|
|
|
|
|
172
|
|
|
$this->removeListFromIndex($key); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param $listElement |
|
180
|
|
|
* @param $data |
|
181
|
|
|
* |
|
182
|
|
|
* @return array|object |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function updateListElementBody($listElement, $data) |
|
185
|
|
|
{ |
|
186
|
|
|
$listElement = $listElement; |
|
187
|
|
|
|
|
188
|
|
|
if (is_string($listElement)) { |
|
189
|
|
|
return $data; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if (is_array($listElement)) { |
|
193
|
|
|
return array_merge((array) $listElement, (array) $data); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return (object) array_merge((array) $listElement, (array) $data); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|