1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Controller\Endpoint\Alerts; |
4
|
|
|
|
5
|
|
|
use League\Fractal\Manager; |
6
|
|
|
use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController; |
7
|
|
|
use Ps2alerts\Api\Repository\AlertRepository; |
8
|
|
|
use Ps2alerts\Api\Transformer\AlertTotalTransformer; |
9
|
|
|
use Ps2alerts\Api\Transformer\AlertTransformer; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
|
13
|
|
|
class AlertEndpointController extends AbstractEndpointController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Construct |
17
|
|
|
* |
18
|
|
|
* @param Ps2alerts\Api\Repository\AlertRepository $repository |
19
|
|
|
* @param Ps2alerts\Api\Transformer\AlertTransformer $transformer |
20
|
|
|
* @param League\Fractal\Manager $fractal |
21
|
|
|
*/ |
22
|
|
|
public function __construct( |
23
|
|
|
AlertRepository $repository, |
24
|
|
|
AlertTransformer $transformer, |
25
|
|
|
Manager $fractal |
26
|
|
|
) { |
27
|
|
|
$this->repository = $repository; |
28
|
|
|
$this->transformer = $transformer; |
29
|
|
|
$this->fractal = $fractal; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns a single alert's information |
34
|
|
|
* |
35
|
|
|
* @param Symfony\Component\HttpFoundation\Request $request |
36
|
|
|
* @param Symfony\Component\HttpFoundation\Response $response |
37
|
|
|
* @param array $args |
38
|
|
|
* |
39
|
|
|
* @return \League\Fractal\TransformerAbstract |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function getSingle(Request $request, Response $response, array $args) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$alert = $this->repository->readSingleById($args['id']); |
44
|
|
|
|
45
|
|
|
if (empty($alert)) { |
46
|
|
|
return $this->errorEmpty($response); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->respond('item', $alert, $this->transformer, $request, $response); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns all currently running alerts |
54
|
|
|
* |
55
|
|
|
* @param Symfony\Component\HttpFoundation\Request $request |
56
|
|
|
* @param Symfony\Component\HttpFoundation\Response $response |
57
|
|
|
* |
58
|
|
|
* @return \League\Fractal\TransformerAbstract |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function getActives(Request $request, Response $response) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$actives = $this->repository->readAllByFields(['InProgress' => 1]); |
63
|
|
|
|
64
|
|
|
if (empty($actives)) { |
65
|
|
|
return $this->errorEmpty($response); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->respond('collection', $actives, $this->transformer, $request, $response); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns all alerts in historial order |
73
|
|
|
* |
74
|
|
|
* @param Symfony\Component\HttpFoundation\Request $request |
75
|
|
|
* @param Symfony\Component\HttpFoundation\Response $response |
76
|
|
|
* |
77
|
|
|
* @return \League\Fractal\TransformerAbstract |
78
|
|
|
*/ |
79
|
|
|
public function getHistoryByDate(Request $request, Response $response) |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$servers = $this->getFiltersFromQueryString($request->get('servers'), 'servers', $response); |
83
|
|
|
$zones = $this->getFiltersFromQueryString($request->get('zones'), 'zones', $response); |
84
|
|
|
$factions = $this->getFiltersFromQueryString($request->get('factions'), 'factions', $response); |
85
|
|
|
$brackets = $this->getFiltersFromQueryString($request->get('brackets'), 'brackets', $response); |
86
|
|
|
} catch (InvalidArgumentException $e) { |
|
|
|
|
87
|
|
|
return $this->errorWrongArgs($response, $e->getMessage()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$dateFrom = $request->get('dateFrom'); |
91
|
|
|
$dateTo = $request->get('dateTo'); |
92
|
|
|
$offset = (int) $request->get('offset'); |
93
|
|
|
$limit = (int) $request->get('limit'); |
94
|
|
|
|
95
|
|
|
// Set defaults if not supplied |
96
|
|
|
if ($offset === null || ! is_numeric($offset)) { |
97
|
|
|
$offset = 0; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($limit === null || ! is_numeric($limit)) { |
101
|
|
|
$limit = 25; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($dateFrom === null) { |
105
|
|
|
$dateFrom = date('Y-m-d H:i:s', strtotime('-48 hours')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($dateTo === null) { |
109
|
|
|
$dateTo = date('Y-m-d H:i:s', strtotime('now')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Format the dates into UNIX timestamp for use with the DB |
113
|
|
|
$dateFrom = date('Y-m-d H:i:s', strtotime($dateFrom)); |
114
|
|
|
$dateTo = date('Y-m-d H:i:s', strtotime($dateTo)); |
115
|
|
|
|
116
|
|
|
$query = $this->repository->newQuery(); |
117
|
|
|
|
118
|
|
|
$query->cols(['*']); |
119
|
|
|
$query->where("`ResultServer` IN ({$servers})"); |
120
|
|
|
$query->where("`ResultAlertCont` IN ({$zones})"); |
121
|
|
|
$query->where("`ResultDateTime` > '{$dateFrom}'"); |
122
|
|
|
$query->where("`ResultDateTime` < '{$dateTo}'"); |
123
|
|
|
$query->where("`ResultWinner` IN ({$factions})"); |
124
|
|
|
$query->where("`ResultTimeType` IN ({$brackets})"); |
125
|
|
|
$query->orderBy(["`ResultEndTime` DESC"]); |
|
|
|
|
126
|
|
|
$query->limit($limit); |
127
|
|
|
$query->offset($offset); |
128
|
|
|
|
129
|
|
|
$history = $this->repository->readRaw($query->getStatement()); |
130
|
|
|
|
131
|
|
|
return $this->respond('collection', $history, $this->transformer, $request, $response); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.