1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Loader; |
4
|
|
|
|
5
|
|
|
use Ps2alerts\Api\QueryObjects\QueryObject; |
6
|
|
|
use Ps2alerts\Api\Loader\AbstractLoader; |
7
|
|
|
use Ps2alerts\Api\Repository\AlertRepository; |
8
|
|
|
|
9
|
|
|
class AlertLoader extends AbstractLoader |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Ps2alerts\Api\Repository\AlertRepository |
13
|
|
|
*/ |
14
|
|
|
protected $repository; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Construct |
18
|
|
|
* |
19
|
|
|
* @param \Ps2alerts\Api\Repository\AlertRepository $repository |
20
|
|
|
*/ |
21
|
|
|
public function __construct(AlertRepository $repository) |
22
|
|
|
{ |
23
|
|
|
$this->repository = $repository; |
24
|
|
|
$this->setCacheNamespace('Alerts'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns recent alerts |
29
|
|
|
* |
30
|
|
|
* @param array $args Path Arguments |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function readRecent(array $args) |
35
|
|
|
{ |
36
|
|
|
$redisKey = "{$this->getCacheNamespace()}:Recent"; |
37
|
|
|
|
38
|
|
|
$this->setCacheExpireTime(3600); // 1 hour |
39
|
|
|
|
40
|
|
|
$queryObject = new QueryObject; |
41
|
|
|
$queryObject->addWhere([ |
42
|
|
|
'col' => 'ResultStartTime', |
43
|
|
|
'op' => '>', |
44
|
|
|
'value' => date('U', strtotime('-48 hours')) |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
if (! empty($args['serverID'])) { |
48
|
|
|
$redisKey .= ":{$args['serverID']}"; |
49
|
|
|
$queryObject->addWhere([ |
50
|
|
|
'col' => 'ResultServer', |
51
|
|
|
'value' => $args['serverID'] |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (! empty($args['limit'])) { |
56
|
|
|
if ($args['limit'] > 50) { |
57
|
|
|
$args['limit'] = 50; |
58
|
|
|
} |
59
|
|
|
$redisKey .= "/{$args['limit']}"; |
60
|
|
|
$queryObject->setLimit($args['limit']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$queryObject->setOrderBy('ResultStartTime'); |
64
|
|
|
$queryObject->setOrderByDirection('desc'); |
65
|
|
|
|
66
|
|
|
return $this->cacheAndReturn( |
67
|
|
|
$this->repository->read($queryObject), |
68
|
|
|
$redisKey |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Reads all currently active alerts |
74
|
|
|
* |
75
|
|
|
* @param array $args |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function readActive($args) |
80
|
|
|
{ |
81
|
|
|
$queryObject = new QueryObject; |
82
|
|
|
$queryObject->addWhere([ |
83
|
|
|
'col' => 'InProgress', |
84
|
|
|
'value' => 1 |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
if (! empty($args['serverID'])) { |
88
|
|
|
$queryObject->addWhere([ |
89
|
|
|
'col' => 'ResultServer', |
90
|
|
|
'value' => $args['serverID'] |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->setCacheable(false); |
95
|
|
|
|
96
|
|
|
return $this->cacheAndReturn( |
97
|
|
|
$this->repository->read($queryObject) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Reads a single Alert |
103
|
|
|
* |
104
|
|
|
* @param integer|string $id |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function readSingle($id) |
109
|
|
|
{ |
110
|
|
|
$redisKey = "{$this->getCacheNamespace()}:{$id}"; |
111
|
|
|
|
112
|
|
|
if ($this->checkRedis($redisKey)) { |
113
|
|
|
return $this->getFromRedis($redisKey); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$queryObject = new QueryObject; |
117
|
|
|
$queryObject->addWhere([ |
118
|
|
|
'col' => 'primary', |
119
|
|
|
'value' => $id |
120
|
|
|
]); |
121
|
|
|
$queryObject->setDimension('single'); |
122
|
|
|
|
123
|
|
|
$result = $this->repository->read($queryObject); |
124
|
|
|
|
125
|
|
|
if ($result['InProgress'] === '1') { |
126
|
|
|
$this->setCacheable(false); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->cacheAndReturn($result, $redisKey); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.