1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\ResultBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
6
|
|
|
use Overwatch\TestBundle\Entity\Test; |
7
|
|
|
use Overwatch\TestBundle\Entity\TestGroup; |
8
|
|
|
use Overwatch\TestBundle\Security\TestGroupVoter; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ApiController |
18
|
|
|
* Handles API request made for Result |
19
|
|
|
* @Route("/api/results") |
20
|
|
|
*/ |
21
|
|
|
class ApiController extends Controller |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns the latest results from across all tests |
25
|
|
|
* |
26
|
|
|
* @Route("") |
27
|
|
|
* @Method({"GET"}) |
28
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
29
|
|
|
* @ApiDoc( |
30
|
|
|
* resource=true, |
31
|
|
|
* filters={ |
32
|
|
|
* {"name"="pageSize", "description"="How many results to return per page","type"="Integer","default"=10,"maximum"=100}, |
33
|
|
|
* {"name"="page", "description"="The page number to return results from","type"="Integer","default"=1} |
34
|
|
|
* }, |
35
|
|
|
* tags={ |
36
|
|
|
* "Super Admin" = "#ff1919" |
37
|
|
|
* } |
38
|
|
|
* ) |
39
|
|
|
*/ |
40
|
1 |
|
public function getResultsAction(Request $request) |
41
|
|
|
{ |
42
|
1 |
|
$size = $request->query->get('pageSize', 10); |
43
|
|
|
|
44
|
1 |
|
$results = $this->getEntityRepository('OverwatchResultBundle:TestResult')->getResults( |
45
|
1 |
|
[], |
46
|
1 |
|
($size >= 100) ? 10 : $size, |
47
|
1 |
|
$request->query->get('page', 1) |
48
|
1 |
|
); |
49
|
|
|
|
50
|
1 |
|
return new JsonResponse($results); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the latest results for each test in the requested group |
55
|
|
|
* |
56
|
|
|
* @Route("/group/{id}") |
57
|
|
|
* @Method({"GET"}) |
58
|
|
|
* @Security("is_granted('view', group)") |
59
|
|
|
* @ApiDoc( |
60
|
|
|
* requirements={ |
61
|
|
|
* {"name"="id", "description"="The ID of the group for which to return results for", "dataType"="integer", "requirement"="\d+"} |
62
|
|
|
* }, |
63
|
|
|
* filters={ |
64
|
|
|
* {"name"="pageSize", "description"="How many results to return per test per page","type"="Integer","default"=10,"maximum"=100}, |
65
|
|
|
* {"name"="page", "description"="The page number to return results from","type"="Integer","default"=1} |
66
|
|
|
* }, |
67
|
|
|
* tags={ |
68
|
|
|
* "Super Admin" = "#ff1919", |
69
|
|
|
* "Admin" = "#ffff33", |
70
|
|
|
* "User" = "#75ff47" |
71
|
|
|
* } |
72
|
|
|
* ) |
73
|
|
|
*/ |
74
|
1 |
|
public function getRecentGroupResultsAction(TestGroup $group, Request $request) |
75
|
|
|
{ |
76
|
1 |
|
$results = []; |
77
|
1 |
|
$size = $request->query->get('pageSize', 10); |
78
|
|
|
|
79
|
1 |
|
foreach ($group->getTests()->toArray() as $test) { |
80
|
1 |
|
$results[] = $this->getEntityRepository('OverwatchResultBundle:TestResult')->getResults( |
81
|
|
|
[ |
82
|
|
|
'test' => $test |
83
|
1 |
|
], |
84
|
1 |
|
($size >= 100) ? 10 : $size, |
85
|
1 |
|
$request->query->get('page', 1) |
86
|
1 |
|
); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
1 |
|
return new JsonResponse($results); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the latest results for the given test |
94
|
|
|
* |
95
|
|
|
* @Route("/test/{id}") |
96
|
|
|
* @Method({"GET"}) |
97
|
|
|
* @ApiDoc( |
98
|
|
|
* filters={ |
99
|
|
|
* {"name"="pageSize", "description"="How many results to return per page","type"="Integer","default"=10,"maximum"=100}, |
100
|
|
|
* {"name"="page", "description"="The page number to return results from","type"="Integer","default"=1} |
101
|
|
|
* }, |
102
|
|
|
* requirements={ |
103
|
|
|
* {"name"="id", "description"="The ID of the test for which to return results for", "dataType"="integer", "requirement"="\d+"} |
104
|
|
|
* }, |
105
|
|
|
* tags={ |
106
|
|
|
* "Super Admin" = "#ff1919", |
107
|
|
|
* "Admin" = "#ffff33", |
108
|
|
|
* "User" = "#75ff47" |
109
|
|
|
* } |
110
|
|
|
* ) |
111
|
|
|
*/ |
112
|
2 |
|
public function getResultsForTestAction(Request $request, Test $test) |
113
|
|
|
{ |
114
|
2 |
|
if (!$this->isGranted(TestGroupVoter::VIEW, $test->getGroup())) { |
115
|
1 |
|
throw $this->createAccessDeniedException('You must be a member of this test\'s group to see it\'s results'); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$size = $request->query->get('pageSize', 10); |
119
|
|
|
|
120
|
1 |
|
$results = $this->getEntityRepository('OverwatchResultBundle:TestResult')->getResults( |
121
|
|
|
[ |
122
|
|
|
'test' => $test |
123
|
1 |
|
], |
124
|
1 |
|
($size >= 100) ? 10 : $size, |
125
|
1 |
|
$request->query->get('page', 1) |
126
|
1 |
|
); |
127
|
|
|
|
128
|
1 |
|
return new JsonResponse($results); |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
private function getEntityRepository($entity) |
132
|
|
|
{ |
133
|
3 |
|
return $this->get('doctrine.orm.entity_manager')->getRepository($entity); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|