1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Loader\Statistics; |
4
|
|
|
|
5
|
|
|
use Ps2alerts\Api\Loader\AbstractLoader; |
6
|
|
|
use Ps2alerts\Api\QueryObjects\QueryObject; |
7
|
|
|
|
8
|
|
|
abstract class AbstractStatisticsLoader extends AbstractLoader |
9
|
|
|
{ |
10
|
|
|
protected $flags; |
11
|
|
|
|
12
|
|
|
public function setFlags($flag) |
13
|
|
|
{ |
14
|
|
|
$this->flags = $flag; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function getFlags() |
18
|
|
|
{ |
19
|
|
|
return $this->flags; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $type; |
26
|
|
|
|
27
|
|
|
public function appendRedisKey($post, $redisKey) |
28
|
|
|
{ |
29
|
|
|
if (! empty($post['wheres'])) { |
30
|
|
|
$whereMD5 = md5($post['wheres']); |
31
|
|
|
$redisKey .= "/{$whereMD5}"; |
32
|
|
|
} |
33
|
|
|
if (! empty($post['orderBy'])) { |
34
|
|
|
$orderMD5 = md5($post['orderBy']); |
35
|
|
|
$redisKey .= "/{$orderMD5}"; |
36
|
|
|
} |
37
|
|
View Code Duplication |
if (! empty($post['limit'])) { |
|
|
|
|
38
|
|
|
// Enforce a max limit |
39
|
|
|
if ($post['limit'] > 50) { |
40
|
|
|
$post['limit'] = 50; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
if (empty($post['limit']) || ! isset($post['limit'])) { |
|
|
|
|
45
|
|
|
$post['limit'] = 10; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$redisKey .= "/{$post['limit']}"; |
49
|
|
|
|
50
|
|
|
return $redisKey; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function processPostVars($post) |
54
|
|
|
{ |
55
|
|
View Code Duplication |
if (! empty($post['wheres'])) { |
|
|
|
|
56
|
|
|
$return['wheres'] = json_decode($post['wheres'], true); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
if (! empty($post['orderBy'])) { |
|
|
|
|
60
|
|
|
$return['orderBy'] = json_decode($post['orderBy'], true); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
if (empty($post['limit']) || ! isset($post['limit'])) { |
|
|
|
|
64
|
|
|
$post['limit'] = 10; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($post['limit'] > 50) { |
68
|
|
|
$post['limit'] = 50; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$return['limit'] = $post['limit']; |
72
|
|
|
|
73
|
|
|
return $return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the top X of a particular statistic |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function readStatistics($post) |
82
|
|
|
{ |
83
|
|
|
$redisKey = "{$this->getCacheNamespace()}:{$this->getType()}"; |
84
|
|
|
$redisKey = $this->appendRedisKey($post, $redisKey); |
85
|
|
|
$post = $this->processPostVars($post); |
86
|
|
|
|
87
|
|
|
if ($this->checkRedis($redisKey)) { |
88
|
|
|
return $this->getFromRedis($redisKey); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$queryObject = new QueryObject; |
92
|
|
|
|
93
|
|
|
foreach ($post['wheres'] as $key => $value) { |
94
|
|
|
$queryObject->addWhere([ |
95
|
|
|
'col' => array_keys($value)[0], |
96
|
|
|
'value' => array_values($value)[0] |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (! empty($post['orderBy'])) { |
101
|
|
|
$queryObject->setOrderBy(array_keys($post['orderBy'])[0]); |
102
|
|
|
$queryObject->setOrderByDirection(array_values($post['orderBy'])[0]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$queryObject->setLimit($post['limit']); |
106
|
|
|
|
107
|
|
|
if (! empty($this->getFlags())) { |
108
|
|
|
// If there are some funky things we have to do, set them. |
109
|
|
|
$queryObject->setFlags($this->getFlags()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->cacheAndReturn( |
113
|
|
|
$this->repository->read($queryObject), |
|
|
|
|
114
|
|
|
$redisKey |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.