1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Controller\Endpoint; |
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
|
|
|
* @see AbstractEndpointController::respondWithItem |
36
|
|
|
* |
37
|
|
|
* @param Symfony\Component\HttpFoundation\Request $request |
38
|
|
|
* @param Symfony\Component\HttpFoundation\Response $response |
39
|
|
|
* @param array $args |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function getSingle(Request $request, Response $response, array $args) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$alert = $this->repository->readSingleById($args['id']); |
46
|
|
|
|
47
|
|
|
if (empty($alert)) { |
48
|
|
|
return $this->errorEmpty($response); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->respond('item', $alert, $this->transformer, $request, $response); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function getActives(Request $request, Response $response) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$actives = $this->repository->readAllByFields(['InProgress', 1]); |
57
|
|
|
|
58
|
|
|
if (empty($actives)) { |
59
|
|
|
return $this->errorEmpty($response); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->respond('collection', $actives, $this->transformer, $request, $response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the victories of each faction and the totals |
67
|
|
|
* |
68
|
|
|
* @param Symfony\Component\HttpFoundation\Request $request |
69
|
|
|
* @param Symfony\Component\HttpFoundation\Response $response |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getVictories(Request $request, Response $response) |
74
|
|
|
{ |
75
|
|
|
$counts = [ |
76
|
|
|
'vs' => $this->repository->readCountByFields(['ResultWinner' => 'VS', 'Valid' => 1]), |
77
|
|
|
'nc' => $this->repository->readCountByFields(['ResultWinner' => 'NC', 'Valid' => 1]), |
78
|
|
|
'tr' => $this->repository->readCountByFields(['ResultWinner' => 'TR', 'Valid' => 1]), |
79
|
|
|
'draw' => $this->repository->readCountByFields(['ResultDraw' => 1, 'Valid' => 1]), |
80
|
|
|
'total' => $this->repository->readCountByFields(['Valid' => 1]) |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
if (empty($counts['total'])) { |
84
|
|
|
return $this->errorEmpty($response); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->respond('item', $counts, new AlertTotalTransformer, $request, $response); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the dominations of each faction and the totals |
92
|
|
|
* |
93
|
|
|
* @param Symfony\Component\HttpFoundation\Request $request |
94
|
|
|
* @param Symfony\Component\HttpFoundation\Response $response |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getDominations(Request $request, Response $response) |
99
|
|
|
{ |
100
|
|
|
$counts = [ |
101
|
|
|
'vs' => $this->repository->readCountByFields(['ResultWinner' => 'VS', 'Valid' => 1, 'ResultDomination' => 1]), |
102
|
|
|
'nc' => $this->repository->readCountByFields(['ResultWinner' => 'NC', 'Valid' => 1, 'ResultDomination' => 1]), |
103
|
|
|
'tr' => $this->repository->readCountByFields(['ResultWinner' => 'TR', 'Valid' => 1, 'ResultDomination' => 1]), |
104
|
|
|
'total' => $this->repository->readCountByFields(['Valid' => 1, 'ResultDomination' => 1]) |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
if (empty($counts['total'])) { |
108
|
|
|
return $this->errorEmpty($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->respond('item', $counts, new AlertTotalTransformer, $request, $response); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.