Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
67 / 67 |
|
100.00% |
25 / 25 |
CRAP | |
100.00% |
1 / 1 |
AbstractAdapter | |
100.00% |
67 / 67 |
|
100.00% |
25 / 25 |
41 | |
100.00% |
1 / 1 |
setModel | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setModelId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getModel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModelId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOriginal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModified | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setUsername | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setUserId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setDomain | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setRoute | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setMethod | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getUsername | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUserId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDomain | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRoute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMethod | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasMetadata | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getMetadata | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
setDiff | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
resolveDiff | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 | |||
hasDiff | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
prepareData | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
send | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getStates | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getStateById | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getStateByModel | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getStateByTimestamp | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getStateByDate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getSnapshot | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Audit\Adapter; |
15 | |
16 | /** |
17 | * Auditor abstract adapter |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Audit |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 1.3.0 |
25 | */ |
26 | abstract class AbstractAdapter implements AdapterInterface |
27 | { |
28 | |
29 | /** |
30 | * Constants for action states |
31 | * @var int |
32 | */ |
33 | const CREATED = 'created'; |
34 | const UPDATED = 'updated'; |
35 | const DELETED = 'deleted'; |
36 | |
37 | /** |
38 | * Model name |
39 | * @var string |
40 | */ |
41 | protected $model = null; |
42 | |
43 | /** |
44 | * Model ID |
45 | * @var int |
46 | */ |
47 | protected $modelId = null; |
48 | |
49 | /** |
50 | * Action (created, updated, deleted) |
51 | * @var string |
52 | */ |
53 | protected $action = null; |
54 | |
55 | /** |
56 | * Original model state differences |
57 | * @var array |
58 | */ |
59 | protected $original = []; |
60 | |
61 | /** |
62 | * Modified model state differences |
63 | * @var array |
64 | */ |
65 | protected $modified = []; |
66 | |
67 | /** |
68 | * Username |
69 | * @var string |
70 | */ |
71 | protected $username = null; |
72 | |
73 | /** |
74 | * User ID |
75 | * @var int |
76 | */ |
77 | protected $userId = null; |
78 | |
79 | /** |
80 | * Domain |
81 | * @var string |
82 | */ |
83 | protected $domain = null; |
84 | |
85 | /** |
86 | * Route |
87 | * @var string |
88 | */ |
89 | protected $route = null; |
90 | |
91 | /** |
92 | * Method |
93 | * @var string |
94 | */ |
95 | protected $method = null; |
96 | |
97 | /** |
98 | * Metadata |
99 | * @var array |
100 | */ |
101 | protected $metadata = []; |
102 | |
103 | /** |
104 | * Set the model name |
105 | * |
106 | * @param string $model |
107 | * @return self |
108 | */ |
109 | public function setModel($model) |
110 | { |
111 | $this->model = $model; |
112 | return $this; |
113 | } |
114 | |
115 | /** |
116 | * Set the model ID |
117 | * |
118 | * @param int $modelId |
119 | * @return self |
120 | */ |
121 | public function setModelId($modelId) |
122 | { |
123 | $this->modelId = $modelId; |
124 | return $this; |
125 | } |
126 | |
127 | /** |
128 | * Get the model name |
129 | * |
130 | * @return string |
131 | */ |
132 | public function getModel() |
133 | { |
134 | return $this->model; |
135 | } |
136 | |
137 | /** |
138 | * Get the model ID |
139 | * |
140 | * @return int |
141 | */ |
142 | public function getModelId() |
143 | { |
144 | return $this->modelId; |
145 | } |
146 | |
147 | /** |
148 | * Get the action |
149 | * |
150 | * @return string |
151 | */ |
152 | public function getAction() |
153 | { |
154 | return $this->action; |
155 | } |
156 | |
157 | /** |
158 | * Get the original model state differences |
159 | * |
160 | * @return array |
161 | */ |
162 | public function getOriginal() |
163 | { |
164 | return $this->original; |
165 | } |
166 | |
167 | /** |
168 | * Get the modified model state differences |
169 | * |
170 | * @return array |
171 | */ |
172 | public function getModified() |
173 | { |
174 | return $this->modified; |
175 | } |
176 | |
177 | /** |
178 | * Set the username |
179 | * |
180 | * @param string $username |
181 | * @return self |
182 | */ |
183 | public function setUsername($username) |
184 | { |
185 | $this->username = $username; |
186 | return $this; |
187 | } |
188 | |
189 | /** |
190 | * Set the user ID |
191 | * |
192 | * @param int $userId |
193 | * @return self |
194 | */ |
195 | public function setUserId($userId) |
196 | { |
197 | $this->userId = $userId; |
198 | return $this; |
199 | } |
200 | |
201 | /** |
202 | * Set the domain |
203 | * |
204 | * @param string $domain |
205 | * @return self |
206 | */ |
207 | public function setDomain($domain) |
208 | { |
209 | $this->domain = $domain; |
210 | return $this; |
211 | } |
212 | |
213 | /** |
214 | * Set the route |
215 | * |
216 | * @param string $route |
217 | * @return self |
218 | */ |
219 | public function setRoute($route) |
220 | { |
221 | $this->route = $route; |
222 | return $this; |
223 | } |
224 | |
225 | /** |
226 | * Set the method |
227 | * |
228 | * @param string $method |
229 | * @return self |
230 | */ |
231 | public function setMethod($method) |
232 | { |
233 | $this->method = $method; |
234 | return $this; |
235 | } |
236 | |
237 | /** |
238 | * Set the metadata |
239 | * |
240 | * @param array $metadata |
241 | * @return self |
242 | */ |
243 | public function setMetadata(array $metadata) |
244 | { |
245 | $this->metadata = $metadata; |
246 | return $this; |
247 | } |
248 | |
249 | /** |
250 | * Add to the metadata |
251 | * |
252 | * @param string $name |
253 | * @param mixed $value |
254 | * @return self |
255 | */ |
256 | public function addMetadata($name, $value) |
257 | { |
258 | $this->metadata[$name] = $value; |
259 | return $this; |
260 | } |
261 | |
262 | /** |
263 | * Get the username |
264 | * |
265 | * @return string |
266 | */ |
267 | public function getUsername() |
268 | { |
269 | return $this->username; |
270 | } |
271 | |
272 | /** |
273 | * Get the user ID |
274 | * |
275 | * @return int |
276 | */ |
277 | public function getUserId() |
278 | { |
279 | return $this->userId; |
280 | } |
281 | |
282 | /** |
283 | * Get the domain |
284 | * |
285 | * @return string |
286 | */ |
287 | public function getDomain() |
288 | { |
289 | return $this->domain; |
290 | } |
291 | |
292 | /** |
293 | * Get the route |
294 | * |
295 | * @return string |
296 | */ |
297 | public function getRoute() |
298 | { |
299 | return $this->route; |
300 | } |
301 | |
302 | /** |
303 | * Get the method |
304 | * |
305 | * @return string |
306 | */ |
307 | public function getMethod() |
308 | { |
309 | return $this->method; |
310 | } |
311 | |
312 | /** |
313 | * Determine if there is metadata |
314 | * |
315 | * @param string $name |
316 | * @return boolean |
317 | */ |
318 | public function hasMetadata($name = null) |
319 | { |
320 | if (null !== $name) { |
321 | return isset($this->metadata[$name]); |
322 | } else { |
323 | return !empty($this->metadata); |
324 | } |
325 | } |
326 | |
327 | /** |
328 | * Get the metadata |
329 | * |
330 | * @param string $name |
331 | * @return mixed |
332 | */ |
333 | public function getMetadata($name = null) |
334 | { |
335 | if (null !== $name) { |
336 | return (isset($this->metadata[$name])) ? $this->metadata[$name] : null; |
337 | } else { |
338 | return $this->metadata; |
339 | } |
340 | } |
341 | |
342 | /** |
343 | * Set the differences in values between the model states (that have already been processed) |
344 | * |
345 | * @param array $old |
346 | * @param array $new |
347 | * @return self |
348 | */ |
349 | public function setDiff(array $old = [], array $new = []) |
350 | { |
351 | $this->original = $old; |
352 | $this->modified = $new; |
353 | |
354 | if (empty($old) && !empty($new)) { |
355 | $this->action = self::CREATED; |
356 | } else if (empty($new) && !empty($old)) { |
357 | $this->action = self::DELETED; |
358 | } else { |
359 | $this->action = self::UPDATED; |
360 | } |
361 | |
362 | return $this; |
363 | } |
364 | |
365 | /** |
366 | * Resolve the differences in values between the model states |
367 | * |
368 | * @param array $old |
369 | * @param array $new |
370 | * @return self |
371 | */ |
372 | public function resolveDiff(array $old = [], array $new = []) |
373 | { |
374 | if (empty($old) && !empty($new)) { |
375 | $this->modified = $new; |
376 | $this->action = self::CREATED; |
377 | } else if (empty($new) && !empty($old)) { |
378 | $this->original = $old; |
379 | $this->action = self::DELETED; |
380 | } else { |
381 | $keys = array_keys(array_diff($old, $new)); |
382 | foreach ($keys as $key) { |
383 | $this->original[$key] = $old[$key]; |
384 | $this->modified[$key] = $new[$key]; |
385 | } |
386 | $this->action = self::UPDATED; |
387 | } |
388 | |
389 | return $this; |
390 | } |
391 | |
392 | /** |
393 | * Check if the model states are different |
394 | * |
395 | * @return boolean |
396 | */ |
397 | public function hasDiff() |
398 | { |
399 | return ((null !== $this->action) && ($this->original !== $this->modified)); |
400 | } |
401 | |
402 | /** |
403 | * Prepare data |
404 | * |
405 | * @param boolean $jsonEncode |
406 | * @return array |
407 | */ |
408 | public function prepareData($jsonEncode = true) |
409 | { |
410 | return [ |
411 | 'user_id' => $this->userId, |
412 | 'username' => $this->username, |
413 | 'domain' => $this->domain, |
414 | 'route' => $this->route, |
415 | 'method' => $this->method, |
416 | 'model' => $this->model, |
417 | 'model_id' => $this->modelId, |
418 | 'action' => $this->action, |
419 | 'old' => ($jsonEncode) ? json_encode($this->original) : $this->original, |
420 | 'new' => ($jsonEncode) ? json_encode($this->modified) : $this->modified, |
421 | 'metadata' => ($jsonEncode) ? json_encode($this->metadata) : $this->metadata, |
422 | 'timestamp' => date('Y-m-d H:i:s') |
423 | ]; |
424 | } |
425 | |
426 | /** |
427 | * Send the results of the audit |
428 | * |
429 | * @return mixed |
430 | */ |
431 | abstract public function send(); |
432 | |
433 | /** |
434 | * Get model states |
435 | * |
436 | * @return array |
437 | */ |
438 | abstract public function getStates(); |
439 | |
440 | /** |
441 | * Get model state by ID |
442 | * |
443 | * @param int $id |
444 | * @return array |
445 | */ |
446 | abstract public function getStateById($id); |
447 | |
448 | /** |
449 | * Get model state by model |
450 | * |
451 | * @param string $model |
452 | * @param int $modelId |
453 | * @return array |
454 | */ |
455 | abstract public function getStateByModel($model, $modelId = null); |
456 | |
457 | /** |
458 | * Get model state by timestamp |
459 | * |
460 | * @param string $from |
461 | * @param string $backTo |
462 | * @return array |
463 | */ |
464 | abstract public function getStateByTimestamp($from, $backTo = null); |
465 | |
466 | /** |
467 | * Get model state by date |
468 | * |
469 | * @param string $from |
470 | * @param string $backTo |
471 | * @return array |
472 | */ |
473 | abstract public function getStateByDate($from, $backTo = null); |
474 | |
475 | /** |
476 | * Get model snapshot by ID |
477 | * |
478 | * @param int $id |
479 | * @param boolean $post |
480 | * @return array |
481 | */ |
482 | abstract public function getSnapshot($id, $post = false); |
483 | } |