1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Loader\Statistics; |
4
|
|
|
|
5
|
|
|
use Ps2alerts\Api\Loader\Statistics\AbstractStatisticsLoader; |
6
|
|
|
use Ps2alerts\Api\QueryObjects\QueryObject; |
7
|
|
|
use Ps2alerts\Api\Repository\AlertRepository; |
8
|
|
|
use Ps2alerts\Api\Validator\AlertInputValidator; |
9
|
|
|
|
10
|
|
|
class AlertStatisticsLoader extends AbstractStatisticsLoader |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Ps2alerts\Api\Repository\AlertRepository |
14
|
|
|
*/ |
15
|
|
|
protected $repository; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Ps2alerts\Api\Validator\AlertInputValidator |
19
|
|
|
*/ |
20
|
|
|
protected $inputValidator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Construct |
24
|
|
|
* |
25
|
|
|
* @param \Ps2alerts\Api\Repository\AlertRepository $repository |
26
|
|
|
* @param \Ps2alerts\Api\Validator\AlertInputValidator $inputValidator |
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
AlertRepository $repository, |
30
|
|
|
AlertInputValidator $inputValidator |
31
|
|
|
) { |
32
|
|
|
$this->repository = $repository; |
33
|
|
|
$this->inputValidator = $inputValidator; |
34
|
|
|
|
35
|
|
|
$this->setCacheNamespace('Statistics'); |
36
|
|
|
$this->setType('Alerts'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Read total counts for alerts |
41
|
|
|
* |
42
|
|
|
* @param array $post |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function readTotals(array $post) |
47
|
|
|
{ |
48
|
|
|
$redisKey = "{$this->getCacheNamespace()}:{$this->getType()}:Totals"; |
49
|
|
|
|
50
|
|
|
$queryObject = new QueryObject; |
51
|
|
|
$queryObject->addSelect('COUNT(ResultID) AS COUNT'); |
52
|
|
|
$queryObject->addWhere([ |
53
|
|
|
'col' => 'Valid', |
54
|
|
|
'value' => '1' |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
View Code Duplication |
if (! empty($post['ResultServer'])) { |
|
|
|
|
58
|
|
|
if ($this->inputValidator->validatePostVars( |
59
|
|
|
'ResultServer', |
60
|
|
|
$post['ResultServer'] |
61
|
|
|
) === true) { |
62
|
|
|
$queryObject->addWhere([ |
63
|
|
|
'col' => 'ResultServer', |
64
|
|
|
'value' => $post['ResultServer'] |
65
|
|
|
]); |
66
|
|
|
$redisKey .= ":Server-{$post['ResultServer']}"; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
if (! empty($post['ResultWinner'])) { |
|
|
|
|
71
|
|
|
if ($this->inputValidator->validatePostVars( |
72
|
|
|
'ResultWinner', |
73
|
|
|
$post['ResultWinner'] |
74
|
|
|
) === true) { |
75
|
|
|
$queryObject->addWhere([ |
76
|
|
|
'col' => 'ResultWinner', |
77
|
|
|
'value' => $post['ResultWinner'] |
78
|
|
|
]); |
79
|
|
|
$redisKey .= ":Winner-{$post['ResultWinner']}"; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
if (! empty($post['ResultAlertCont'])) { |
|
|
|
|
84
|
|
|
if ($this->inputValidator->validatePostVars( |
85
|
|
|
'ResultAlertCont', |
86
|
|
|
$post['ResultAlertCont'] |
87
|
|
|
) === true) { |
88
|
|
|
$queryObject->addWhere([ |
89
|
|
|
'col' => 'ResultAlertCont', |
90
|
|
|
'value' => $post['ResultAlertCont'] |
91
|
|
|
]); |
92
|
|
|
$redisKey .= ":Cont-{$post['ResultAlertCont']}"; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
if (! empty($post['ResultDomination'])) { |
|
|
|
|
97
|
|
|
if ($this->inputValidator->validatePostVars( |
98
|
|
|
'ResultDomination', |
99
|
|
|
$post['ResultDomination'] |
100
|
|
|
) === true) { |
101
|
|
|
$queryObject->addWhere([ |
102
|
|
|
'col' => 'ResultDomination', |
103
|
|
|
'value' => $post['ResultDomination'] |
104
|
|
|
]); |
105
|
|
|
$redisKey .= ":Domination-{$post['ResultDomination']}"; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($this->checkRedis($redisKey)) { |
110
|
|
|
return $this->getFromRedis($redisKey); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->setCacheExpireTime(900); // 15 mins |
114
|
|
|
|
115
|
|
|
return $this->cacheAndReturn( |
116
|
|
|
$this->repository->read($queryObject), |
117
|
|
|
$redisKey |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.