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