1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use AppBundle\Model\Link; |
6
|
|
|
use AppBundle\Model\PaginationLinks; |
7
|
|
|
use FOS\RestBundle\Request\ParamFetcher; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
9
|
|
|
use FOS\RestBundle\Controller\Annotations\View as RestView; |
10
|
|
|
use FOS\RestBundle\Controller\Annotations\RouteResource; |
11
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam; |
12
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
13
|
|
|
use AppBundle\Model\EmployeesResponse; |
14
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @RouteResource("Employee") |
18
|
|
|
*/ |
19
|
|
|
class EmployeesController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @ApiDoc( |
23
|
|
|
* resource=true, |
24
|
|
|
* description="Returns a collection of theatre employees.", |
25
|
|
|
* statusCodes={ |
26
|
|
|
* 200="Returned when all parameters were correct", |
27
|
|
|
* 404="Returned when the entities with given limit and offset are not found", |
28
|
|
|
* }, |
29
|
|
|
* output = "array<AppBundle\Model\EmployeesResponse>" |
30
|
|
|
* ) |
31
|
|
|
* |
32
|
|
|
* @QueryParam(name="limit", requirements="\d+", default="10", description="Count entries at one page") |
33
|
|
|
* @QueryParam(name="page", requirements="\d+", default="1", description="Number of page to be shown") |
34
|
|
|
* @QueryParam( |
35
|
|
|
* name="locale", |
36
|
|
|
* requirements="^[a-zA-Z]+", |
37
|
|
|
* default="uk", |
38
|
|
|
* description="Selects language of data you want to receive" |
39
|
|
|
* ) |
40
|
|
|
* |
41
|
|
|
* @RestView |
42
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
43
|
|
|
*/ |
44
|
2 |
|
public function cgetAction(ParamFetcher $paramFetcher) |
45
|
|
|
{ |
46
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
47
|
|
|
|
48
|
|
|
$employees = $em |
49
|
2 |
|
->getRepository('AppBundle:Employee') |
50
|
2 |
|
->findBy( |
51
|
2 |
|
[], |
52
|
2 |
|
['lastName' => 'ASC'], |
53
|
2 |
|
$paramFetcher->get('limit'), |
54
|
2 |
|
($paramFetcher->get('page')-1) * $paramFetcher->get('limit') |
55
|
|
|
) |
56
|
|
|
; |
57
|
|
|
|
58
|
2 |
|
$employeesTranslated = array(); |
59
|
|
|
|
60
|
2 |
|
foreach ($employees as $employee) { |
61
|
2 |
|
$employee->setLocale($paramFetcher->get('locale')); |
62
|
|
|
|
63
|
2 |
|
$em->refresh($employee); |
64
|
|
|
|
65
|
2 |
|
if ($employee->getTranslations()) { |
66
|
2 |
|
$employee->unsetTranslations(); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$this->get('translator')->setLocale($paramFetcher->get('locale')); |
70
|
2 |
|
$employee->setPosition($this->get('translator')->trans($employee->getPosition())); |
71
|
|
|
|
72
|
2 |
|
$employeesTranslated[] = $employee; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
$employees = $employeesTranslated; |
76
|
|
|
|
77
|
2 |
|
$employeesResponse = new EmployeesResponse(); |
78
|
2 |
|
$employeesResponse->setEmployees($employees); |
79
|
2 |
|
$employeesResponse->setTotalCount( |
80
|
2 |
|
$this->getDoctrine()->getManager()->getRepository('AppBundle:Employee')->getCount() |
81
|
|
|
); |
82
|
2 |
|
$employeesResponse->setPageCount(ceil($employeesResponse->getTotalCount() / $paramFetcher->get('limit'))); |
83
|
2 |
|
$employeesResponse->setPage($paramFetcher->get('page')); |
84
|
|
|
|
85
|
2 |
|
$self = $this->generateUrl( |
86
|
2 |
|
'get_employees', |
87
|
|
|
[ |
88
|
2 |
|
'locale' => $paramFetcher->get('locale'), |
89
|
2 |
|
'limit' => $paramFetcher->get('limit'), |
90
|
2 |
|
'page' => $paramFetcher->get('page'), |
91
|
|
|
], |
92
|
2 |
|
UrlGeneratorInterface::ABSOLUTE_URL |
93
|
|
|
); |
94
|
|
|
|
95
|
2 |
|
$first = $this->generateUrl( |
96
|
2 |
|
'get_employees', |
97
|
|
|
[ |
98
|
2 |
|
'locale' => $paramFetcher->get('locale'), |
99
|
2 |
|
'limit' => $paramFetcher->get('limit'), |
100
|
|
|
], |
101
|
2 |
|
UrlGeneratorInterface::ABSOLUTE_URL |
102
|
|
|
); |
103
|
|
|
|
104
|
2 |
|
$nextPage = $paramFetcher->get('page') < $employeesResponse->getPageCount() ? |
105
|
2 |
|
$this->generateUrl( |
106
|
2 |
|
'get_employees', |
107
|
|
|
[ |
108
|
2 |
|
'locale' => $paramFetcher->get('locale'), |
109
|
2 |
|
'limit' => $paramFetcher->get('limit'), |
110
|
2 |
|
'page' => $paramFetcher->get('page')+1, |
111
|
|
|
], |
112
|
2 |
|
UrlGeneratorInterface::ABSOLUTE_URL |
113
|
|
|
) : |
114
|
2 |
|
'false'; |
115
|
|
|
|
116
|
2 |
|
$previsiousPage = $paramFetcher->get('page') > 1 ? |
117
|
|
|
$this->generateUrl( |
118
|
|
|
'get_employees', |
119
|
|
|
[ |
120
|
|
|
'locale' => $paramFetcher->get('locale'), |
121
|
|
|
'limit' => $paramFetcher->get('limit'), |
122
|
|
|
'page' => $paramFetcher->get('page')-1, |
123
|
|
|
], |
124
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
125
|
|
|
) : |
126
|
2 |
|
'false'; |
127
|
|
|
|
128
|
2 |
|
$last = $this->generateUrl( |
129
|
2 |
|
'get_employees', |
130
|
|
|
[ |
131
|
2 |
|
'locale' => $paramFetcher->get('locale'), |
132
|
2 |
|
'limit' => $paramFetcher->get('limit'), |
133
|
2 |
|
'page' => $employeesResponse->getPageCount(), |
134
|
|
|
], |
135
|
2 |
|
UrlGeneratorInterface::ABSOLUTE_URL |
136
|
|
|
); |
137
|
|
|
|
138
|
2 |
|
$links = new PaginationLinks(); |
139
|
|
|
|
140
|
2 |
|
$employeesResponse->setLinks($links->setSelf(new Link($self))); |
141
|
2 |
|
$employeesResponse->setLinks($links->setFirst(new Link($first))); |
142
|
2 |
|
$employeesResponse->setLinks($links->setNext(new Link($nextPage))); |
143
|
2 |
|
$employeesResponse->setLinks($links->setPrev(new Link($previsiousPage))); |
144
|
2 |
|
$employeesResponse->setLinks($links->setLast(new Link($last))); |
145
|
|
|
|
146
|
2 |
|
return $employeesResponse; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @ApiDoc( |
151
|
|
|
* resource=true, |
152
|
|
|
* description="Returns an Employee by unique property {slug}", |
153
|
|
|
* statusCodes={ |
154
|
|
|
* 200="Returned when employee by {slug} found in database" , |
155
|
|
|
* 404="Returned when employee by {slug} not found in database", |
156
|
|
|
* }, |
157
|
|
|
* output = "AppBundle\Entity\Employee" |
158
|
|
|
* ) |
159
|
|
|
* |
160
|
|
|
* @QueryParam( |
161
|
|
|
* name="locale", |
162
|
|
|
* requirements="^[a-zA-Z]+", |
163
|
|
|
* default="uk", |
164
|
|
|
* description="Selects language of data you want to receive" |
165
|
|
|
* ) |
166
|
|
|
* |
167
|
|
|
* @RestView |
168
|
|
|
*/ |
169
|
1 |
|
public function getAction(ParamFetcher $paramFetcher, $slug) |
170
|
|
|
{ |
171
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
172
|
|
|
|
173
|
|
|
$employee = $em-> |
174
|
1 |
|
getRepository('AppBundle:Employee')->findOneByslug($slug); |
175
|
|
|
|
176
|
1 |
|
if (!$employee) { |
177
|
|
|
throw $this->createNotFoundException('Unable to find '.$slug.' entity'); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
$employee->setLocale($paramFetcher->get('locale')); |
181
|
1 |
|
$em->refresh($employee); |
182
|
|
|
|
183
|
1 |
|
if ($employee->getTranslations()) { |
184
|
1 |
|
$employee->unsetTranslations(); |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
$this->get('translator')->setLocale($paramFetcher->get('locale')); |
188
|
1 |
|
$employee->setPosition($this->get('translator')->trans($employee->getPosition())); |
189
|
|
|
|
190
|
1 |
|
return $employee; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @ApiDoc( |
195
|
|
|
* resource=true, |
196
|
|
|
* description="Returns Employee roles by his slug", |
197
|
|
|
* statusCodes={ |
198
|
|
|
* 200="Returned when employee by {slug} found in database" , |
199
|
|
|
* 404="Returned when employee by {slug} not found in database", |
200
|
|
|
* }, |
201
|
|
|
* output = "array<AppBundle\Entity\Role>" |
202
|
|
|
* ) |
203
|
|
|
* |
204
|
|
|
* @QueryParam( |
205
|
|
|
* name="locale", |
206
|
|
|
* requirements="^[a-zA-Z]+", |
207
|
|
|
* default="uk", |
208
|
|
|
* description="Selects language of data you want to receive" |
209
|
|
|
* ) |
210
|
|
|
* |
211
|
|
|
* @RestView |
212
|
|
|
*/ |
213
|
1 |
|
public function getRolesAction(ParamFetcher $paramFetcher, $slug) |
214
|
|
|
{ |
215
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
216
|
|
|
|
217
|
|
|
$employee = $em |
218
|
1 |
|
->getRepository('AppBundle:Employee')->findOneByslug($slug); |
219
|
|
|
|
220
|
1 |
|
if (!$employee) { |
221
|
|
|
throw $this->createNotFoundException('Unable to find '.$slug.' entity'); |
222
|
|
|
} |
223
|
|
|
|
224
|
1 |
|
$employee->setLocale($paramFetcher->get('locale')); |
225
|
1 |
|
$em->refresh($employee); |
226
|
|
|
|
227
|
1 |
|
if ($employee->getTranslations()) { |
228
|
1 |
|
$employee->unsetTranslations(); |
229
|
|
|
} |
230
|
|
|
|
231
|
1 |
|
$this->get('translator')->setLocale($paramFetcher->get('locale')); |
232
|
1 |
|
$employee->setPosition($this->get('translator')->trans($employee->getPosition())); |
233
|
|
|
|
234
|
1 |
|
$roles = $employee->getRoles(); |
235
|
|
|
|
236
|
1 |
|
$rolesTranslated = []; |
237
|
|
|
|
238
|
1 |
View Code Duplication |
foreach ($roles as $role) { |
|
|
|
|
239
|
|
|
$role->setLocale($paramFetcher->get('locale')); |
240
|
|
|
|
241
|
|
|
$performance = $role->getPerformance(); |
242
|
|
|
$performance->setLocale($paramFetcher->get('locale')); |
243
|
|
|
|
244
|
|
|
$em->refresh($role); |
245
|
|
|
$em->refresh($performance); |
246
|
|
|
|
247
|
|
|
if ($role->getTranslations()) { |
248
|
|
|
$role->unsetTranslations(); |
249
|
|
|
} |
250
|
|
|
if ($performance->getTranslations()) { |
251
|
|
|
$performance->unsetTranslations(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$rolesTranslated[] = $role; |
255
|
|
|
} |
256
|
|
|
|
257
|
1 |
|
$roles = $rolesTranslated; |
258
|
|
|
|
259
|
1 |
|
return $roles; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
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.