1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Api; |
4
|
|
|
|
5
|
|
|
use App\Model\Link; |
6
|
|
|
use App\Model\PaginationLinks; |
7
|
|
|
use App\Model\PerformancesResponse; |
8
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam; |
9
|
|
|
use FOS\RestBundle\Request\ParamFetcher; |
10
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
11
|
|
|
use Swagger\Annotations as SWG; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @Route("/api/performances") |
17
|
|
|
*/ |
18
|
|
|
class PerformancesController extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @Route("", name="get_performances", methods={"GET"}) |
22
|
|
|
* @SWG\Response( |
23
|
|
|
* response=200, |
24
|
|
|
* description="Returns when all parameters were correct", |
25
|
|
|
* @SWG\Schema( |
26
|
|
|
* type="array", |
27
|
|
|
* @SWG\Items(ref=@Model(type=PerformancesResponse::class)) |
28
|
|
|
* ) |
29
|
|
|
* ) |
30
|
|
|
* @SWG\Response( |
31
|
|
|
* response=404, |
32
|
|
|
* description="Returns when the entity is not found", |
33
|
|
|
* ) |
34
|
|
|
* |
35
|
|
|
* @QueryParam(name="limit", requirements="\d+", default="10", description="Count entries at one page") |
36
|
|
|
* @QueryParam(name="page", requirements="\d+", default="1", description="Number of page to be shown") |
37
|
|
|
* @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
38
|
|
|
*/ |
39
|
2 |
|
public function getList(ParamFetcher $paramFetcher) |
40
|
|
|
{ |
41
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
42
|
|
|
|
43
|
|
|
$performances = $em |
44
|
2 |
|
->getRepository('App:Performance') |
45
|
2 |
|
->findBy( |
46
|
2 |
|
['festival' => null], |
47
|
2 |
|
['premiere' => 'DESC'], |
48
|
2 |
|
$paramFetcher->get('limit'), |
49
|
2 |
|
($paramFetcher->get('page')-1) * $paramFetcher->get('limit') |
50
|
|
|
); |
51
|
|
|
|
52
|
2 |
|
$performancesTranslated = array(); |
53
|
|
|
|
54
|
2 |
View Code Duplication |
foreach ($performances as $performance) { |
|
|
|
|
55
|
2 |
|
$performance->setLocale($paramFetcher->get('locale')); |
56
|
2 |
|
$em->refresh($performance); |
57
|
|
|
|
58
|
2 |
|
if ($performance->getTranslations()) { |
59
|
2 |
|
$performance->unsetTranslations(); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$performancesTranslated[] = $performance; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$performances = $performancesTranslated; |
66
|
|
|
|
67
|
2 |
|
$performancesResponse = new PerformancesResponse(); |
68
|
2 |
|
$performancesResponse->setPerformances($performances); |
69
|
2 |
|
$performancesResponse->setTotalCount($this->getDoctrine()->getManager()->getRepository('App:Performance')->getCount()); |
|
|
|
|
70
|
2 |
|
$performancesResponse->setPageCount(ceil($performancesResponse->getTotalCount() / $paramFetcher->get('limit'))); |
71
|
2 |
|
$performancesResponse->setPage($paramFetcher->get('page')); |
72
|
|
|
|
73
|
2 |
|
$self = $this->generateUrl('get_performances', [ |
74
|
2 |
|
'locale' => $paramFetcher->get('locale'), |
75
|
2 |
|
'limit' => $paramFetcher->get('limit'), |
76
|
2 |
|
'page' => $paramFetcher->get('page'), |
77
|
2 |
|
], true |
|
|
|
|
78
|
|
|
); |
79
|
|
|
|
80
|
2 |
|
$first = $this->generateUrl('get_performances', [ |
81
|
2 |
|
'locale' => $paramFetcher->get('locale'), |
82
|
2 |
|
'limit' => $paramFetcher->get('limit'), |
83
|
2 |
|
], true |
|
|
|
|
84
|
|
|
); |
85
|
|
|
|
86
|
2 |
|
$nextPage = $paramFetcher->get('page') < $performancesResponse->getPageCount() ? |
87
|
1 |
|
$this->generateUrl('get_performances', [ |
88
|
1 |
|
'locale' => $paramFetcher->get('locale'), |
89
|
1 |
|
'limit' => $paramFetcher->get('limit'), |
90
|
1 |
|
'page' => $paramFetcher->get('page')+1, |
91
|
1 |
|
], true |
|
|
|
|
92
|
|
|
) : |
93
|
2 |
|
'false'; |
94
|
|
|
|
95
|
2 |
|
$previsiousPage = $paramFetcher->get('page') > 1 ? |
96
|
|
|
$this->generateUrl('get_performances', [ |
97
|
|
|
'locale' => $paramFetcher->get('locale'), |
98
|
|
|
'limit' => $paramFetcher->get('limit'), |
99
|
|
|
'page' => $paramFetcher->get('page')-1, |
100
|
|
|
], true |
|
|
|
|
101
|
|
|
) : |
102
|
2 |
|
'false'; |
103
|
|
|
|
104
|
2 |
|
$last = $this->generateUrl('get_performances', [ |
105
|
2 |
|
'locale' => $paramFetcher->get('locale'), |
106
|
2 |
|
'limit' => $paramFetcher->get('limit'), |
107
|
2 |
|
'page' => $performancesResponse->getPageCount(), |
108
|
2 |
|
], true |
|
|
|
|
109
|
|
|
); |
110
|
|
|
|
111
|
2 |
|
$links = new PaginationLinks(); |
112
|
|
|
|
113
|
2 |
|
$performancesResponse->setLinks($links->setSelf(new Link($self))); |
114
|
2 |
|
$performancesResponse->setLinks($links->setFirst(new Link($first))); |
115
|
2 |
|
$performancesResponse->setLinks($links->setNext(new Link($nextPage))); |
116
|
2 |
|
$performancesResponse->setLinks($links->setPrev(new Link($previsiousPage))); |
117
|
2 |
|
$performancesResponse->setLinks($links->setLast(new Link($last))); |
118
|
|
|
|
119
|
2 |
|
foreach ($performances as $performance) { |
120
|
2 |
|
$performance->setLinks([ |
121
|
2 |
|
['rel' => 'self', 'href' => $this->generateUrl('get_performance', ['slug' => $performance->getSlug()], true)], |
|
|
|
|
122
|
2 |
|
['rel' => 'self.roles', 'href' => $this->generateUrl('get_performance_roles', ['slug' => $performance->getSlug()], true)], |
|
|
|
|
123
|
2 |
|
['rel' => 'self.events', 'href' => $this->generateUrl('get_performanceevents', ['performance' => $performance->getSlug()], true)], |
|
|
|
|
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
return $performancesResponse; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @Route("/{slug}", name="get_performance", methods={"GET"}) |
132
|
|
|
* @SWG\Response( |
133
|
|
|
* response=200, |
134
|
|
|
* description="Returns Performance by unique property {slug}", |
135
|
|
|
* @Model(type=Performance::class) |
136
|
|
|
* ) |
137
|
|
|
* @SWG\Response( |
138
|
|
|
* response=404, |
139
|
|
|
* description="Returns when Performance was not found in database", |
140
|
|
|
* ) |
141
|
|
|
* |
142
|
|
|
* @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
143
|
|
|
*/ |
144
|
1 |
View Code Duplication |
public function getAction(ParamFetcher $paramFetcher, $slug) |
|
|
|
|
145
|
|
|
{ |
146
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
147
|
|
|
|
148
|
|
|
$performance = $em |
149
|
1 |
|
->getRepository('App:Performance')->findOneByslug($slug); |
150
|
|
|
|
151
|
1 |
|
if (!$performance) { |
152
|
1 |
|
throw $this->createNotFoundException('Unable to find '.$slug.' entity'); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
$performance->setLocale($paramFetcher->get('locale')); |
156
|
1 |
|
$em->refresh($performance); |
157
|
|
|
|
158
|
1 |
|
if ($performance->getTranslations()) { |
159
|
1 |
|
$performance->unsetTranslations(); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $performance; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @Route("/{slug}/roles", name="get_performance_roles", methods={"GET"}) |
167
|
|
|
* @SWG\Response( |
168
|
|
|
* response=200, |
169
|
|
|
* description="Returns Performance roles by his unique {slug}", |
170
|
|
|
* @SWG\Schema( |
171
|
|
|
* type="array", |
172
|
|
|
* @SWG\Items(ref=@Model(type=Role::class)) |
173
|
|
|
* ) |
174
|
|
|
* ) |
175
|
|
|
* @SWG\Response( |
176
|
|
|
* response=404, |
177
|
|
|
* description="Returns when Performance by slug was not found in database", |
178
|
|
|
* ) |
179
|
|
|
* |
180
|
|
|
* @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
181
|
|
|
*/ |
182
|
1 |
|
public function getRolesAction(ParamFetcher $paramFetcher, $slug) |
183
|
|
|
{ |
184
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
185
|
|
|
|
186
|
|
|
$performance = $em |
187
|
1 |
|
->getRepository('App:Performance')->findOneByslug($slug); |
188
|
|
|
|
189
|
1 |
|
if (!$performance) { |
190
|
1 |
|
throw $this->createNotFoundException('Unable to find '.$slug.' entity'); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
$performance->setLocale($paramFetcher->get('locale')); |
194
|
1 |
|
$em->refresh($performance); |
195
|
|
|
|
196
|
1 |
|
if ($performance->getTranslations()) { |
197
|
1 |
|
$performance->unsetTranslations(); |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
$roles = $performance->getRoles(); |
201
|
1 |
|
$rolesTrans = []; |
202
|
|
|
|
203
|
1 |
View Code Duplication |
foreach ($roles as $role) { |
|
|
|
|
204
|
1 |
|
$role->setLocale($paramFetcher->get('locale')); |
205
|
1 |
|
$em->refresh($role); |
206
|
|
|
|
207
|
1 |
|
if ($role->getTranslations()) { |
208
|
1 |
|
$role->unsetTranslations(); |
209
|
|
|
} |
210
|
|
|
|
211
|
1 |
|
$role->getEmployee()->setLocale($paramFetcher->get('locale')); |
212
|
1 |
|
$em->refresh($role->getEmployee()); |
213
|
|
|
|
214
|
1 |
|
if ($role->getEmployee()->getTranslations()) { |
215
|
1 |
|
$role->getEmployee()->unsetTranslations(); |
216
|
|
|
} |
217
|
|
|
|
218
|
1 |
|
$rolesTrans[] = $role; |
219
|
|
|
} |
220
|
1 |
|
$roles = $rolesTrans; |
221
|
|
|
|
222
|
1 |
|
return $roles; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @Route("/{slug}/performanceevents", name="get_performance_performance_events", methods={"GET"}) |
227
|
|
|
* @SWG\Response( |
228
|
|
|
* response=200, |
229
|
|
|
* description="Returns Performance events by Performance {slug}", |
230
|
|
|
* @SWG\Schema( |
231
|
|
|
* type="array", |
232
|
|
|
* @SWG\Items(ref=@Model(type=PerformanceEvent::class)) |
233
|
|
|
* ) |
234
|
|
|
* ) |
235
|
|
|
* @SWG\Response( |
236
|
|
|
* response=404, |
237
|
|
|
* description="Returns when Performance by {slug} was not found in database", |
238
|
|
|
* ) |
239
|
|
|
* @SWG\Get(deprecated=true) |
240
|
|
|
* |
241
|
|
|
* @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
242
|
|
|
*/ |
243
|
1 |
|
public function getPerformanceEventsAction(ParamFetcher $paramFetcher, $slug) |
244
|
|
|
{ |
245
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
246
|
|
|
|
247
|
1 |
|
$performance = $em->getRepository('App:Performance')->findOneByslug($slug); |
248
|
|
|
|
249
|
1 |
|
if (!$performance) { |
250
|
1 |
|
throw $this->createNotFoundException('Unable to find '.$slug.' entity'); |
251
|
|
|
} |
252
|
|
|
|
253
|
1 |
|
$performance->setLocale($paramFetcher->get('locale')); |
254
|
1 |
|
$em->refresh($performance); |
255
|
|
|
|
256
|
1 |
|
if ($performance->getTranslations()) { |
257
|
1 |
|
$performance->unsetTranslations(); |
258
|
|
|
} |
259
|
|
|
|
260
|
1 |
|
$performanceEvents = $performance->getPerformanceEvents(); |
261
|
1 |
|
$performanceEventsTrans = []; |
262
|
|
|
|
263
|
1 |
View Code Duplication |
foreach ($performanceEvents as $performanceEvent) { |
|
|
|
|
264
|
1 |
|
$performanceEvent->setLocale($paramFetcher->get('locale')); |
265
|
1 |
|
$em->refresh($performanceEvent); |
266
|
1 |
|
if ($performanceEvent->getTranslations()) { |
267
|
1 |
|
$performanceEvent->unsetTranslations(); |
268
|
|
|
} |
269
|
1 |
|
$performanceEventsTrans[] = $performanceEvent; |
270
|
|
|
} |
271
|
1 |
|
$performanceEvents = $performanceEventsTrans; |
272
|
|
|
|
273
|
1 |
|
return $performanceEvents; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
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.