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
|
|
|
/** |
11
|
|
|
* Flags set for workarounds |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $flags; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Allows setting of workaround flags |
19
|
|
|
* |
20
|
|
|
* @param string $flag |
21
|
|
|
*/ |
22
|
|
|
public function setFlags($flag) |
23
|
|
|
{ |
24
|
|
|
$this->flags = $flag; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Retrieves workaround flags |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function getFlags() |
33
|
|
|
{ |
34
|
|
|
return $this->flags; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $type; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the top X of a particular statistic |
44
|
|
|
* |
45
|
|
|
* @param array $post POST variables from the request |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function readStatistics($post) |
50
|
|
|
{ |
51
|
|
|
$redisKey = "{$this->getCacheNamespace()}:{$this->getType()}"; |
52
|
|
|
$redisKey = $this->appendRedisKey($post, $redisKey); |
53
|
|
|
$post = $this->processPostVars($post); |
54
|
|
|
|
55
|
|
|
if ($this->checkRedis($redisKey)) { |
56
|
|
|
return $this->getFromRedis($redisKey); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$queryObject = new QueryObject; |
60
|
|
|
$queryObject = $this->setupQueryObject($queryObject, $post); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return $this->cacheAndReturn( |
63
|
|
|
$this->repository->read($queryObject), |
|
|
|
|
64
|
|
|
$redisKey |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Build a redis key based off inputs provided by the POST request |
70
|
|
|
* |
71
|
|
|
* @param array $post |
72
|
|
|
* @param string $redisKey Redis Key to append to |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function appendRedisKey($post, $redisKey) |
77
|
|
|
{ |
78
|
|
|
if (! empty($post['selects'])) { |
79
|
|
|
$whereMD5 = md5($post['selects']); |
80
|
|
|
$redisKey .= "/select{$whereMD5}"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (! empty($post['wheres'])) { |
84
|
|
|
$whereMD5 = md5($post['wheres']); |
85
|
|
|
$redisKey .= "/where{$whereMD5}"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (! empty($post['whereIns'])) { |
89
|
|
|
$whereInMD5 = md5($post['whereIns']); |
90
|
|
|
$redisKey .= "/whereIn{$whereInMD5}"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (! empty($post['orderBy'])) { |
94
|
|
|
$orderMD5 = md5($post['orderBy']); |
95
|
|
|
$redisKey .= "/order{$orderMD5}"; |
96
|
|
|
} |
97
|
|
View Code Duplication |
if (! empty($post['limit'])) { |
|
|
|
|
98
|
|
|
// Enforce a max limit |
99
|
|
|
if ($post['limit'] > 50) { |
100
|
|
|
$post['limit'] = 50; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
if (empty($post['limit']) || ! isset($post['limit'])) { |
|
|
|
|
105
|
|
|
$post['limit'] = 10; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$redisKey .= "/limit{$post['limit']}"; |
109
|
|
|
|
110
|
|
|
return $redisKey; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* De-encode the POST vars for use |
115
|
|
|
* |
116
|
|
|
* @param array $post |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function processPostVars($post) |
121
|
|
|
{ |
122
|
|
|
if (! empty($post['wheres'])) { |
123
|
|
|
$return['wheres'] = json_decode($post['wheres'], true); |
|
|
|
|
124
|
|
|
$this->getLogDriver()->addDebug(json_encode($return['wheres'])); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (! empty($post['whereIns'])) { |
128
|
|
|
$return['whereIns'] = json_decode($post['whereIns'], true); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (! empty($post['orderBy'])) { |
132
|
|
|
$return['orderBy'] = json_decode($post['orderBy'], true); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
if (empty($post['limit']) || ! isset($post['limit'])) { |
|
|
|
|
136
|
|
|
$post['limit'] = 10; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($post['limit'] > 50) { |
140
|
|
|
$post['limit'] = 50; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$return['limit'] = $post['limit']; |
144
|
|
|
|
145
|
|
|
return $return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Takes common requests and appends them to the query object. Any other |
150
|
|
|
* special requirements will be handled after |
151
|
|
|
* |
152
|
|
|
* @param Ps2alerts\Api\QueryObjects\QueryObject $queryObject |
153
|
|
|
* @param array $post |
154
|
|
|
* |
155
|
|
|
* @return Ps2alerts\Api\QueryObjects\QueryObject |
156
|
|
|
*/ |
157
|
|
|
public function setupQueryObject($queryObject, $post) |
158
|
|
|
{ |
159
|
|
|
if (! empty($post['wheres'])) { |
160
|
|
|
foreach ($post['wheres'] as $key => $value) { |
161
|
|
|
$queryObject->addWhere([ |
162
|
|
|
'col' => $key, |
163
|
|
|
'value' => $value |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (! empty($post['whereIns'])) { |
169
|
|
|
foreach ($post['whereIns'] as $key => $value) { |
170
|
|
|
// Escape strings manually, incase of player IDs etc |
171
|
|
|
foreach ($value as $i => $val) { |
172
|
|
|
if (is_string($val)) { |
173
|
|
|
$value[$i] = "'{$val}'"; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$queryObject->addWhereIn([ |
178
|
|
|
'col' => $key, |
179
|
|
|
'value' => implode(',', $value) // use implode for WHERE IN (x,x) |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if (! empty($post['orderBy'])) { |
185
|
|
|
$queryObject->setOrderBy(array_keys($post['orderBy'])[0]); |
186
|
|
|
$queryObject->setOrderByDirection(array_values($post['orderBy'])[0]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (! empty($post['limit'])) { |
190
|
|
|
$queryObject->setLimit($post['limit']); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (! empty($this->getFlags())) { |
194
|
|
|
// If there are some funky things we have to do, set them. |
195
|
|
|
$queryObject->setFlags($this->getFlags()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// This should always be set |
199
|
|
|
$queryObject->addWhere([ |
200
|
|
|
'col' => 'Valid', |
201
|
|
|
'value' => '1' |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
return $queryObject; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.