|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller\Api; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Employee; |
|
6
|
|
|
use App\Entity\Performance; |
|
7
|
|
|
use App\Entity\Role; |
|
8
|
|
|
use App\Model\EmployeesResponse; |
|
9
|
|
|
use App\Model\PerformancesResponse; |
|
10
|
|
|
use FOS\RestBundle\Request\ParamFetcher; |
|
11
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam; |
|
12
|
|
|
use JMS\Serializer\SerializerInterface; |
|
13
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
|
14
|
|
|
use Swagger\Annotations as SWG; |
|
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
17
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @Route("/api/employees") |
|
21
|
|
|
*/ |
|
22
|
|
|
class EmployeesController extends AbstractController |
|
23
|
|
|
{ |
|
24
|
|
|
private TranslatorInterface $translator; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function __construct(TranslatorInterface $translator) |
|
27
|
|
|
{ |
|
28
|
4 |
|
$this->translator = $translator; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
4 |
|
* @Route("", name="get_employees", methods={"GET"}) |
|
33
|
4 |
|
* @SWG\Response( |
|
34
|
4 |
|
* response=200, |
|
35
|
|
|
* description="Returns a collection of theatre employees.", |
|
36
|
|
|
* @SWG\Schema(ref=@Model(type=EmployeesResponse::class)) |
|
37
|
|
|
* ) |
|
38
|
|
|
* @SWG\Response( |
|
39
|
|
|
* response=404, |
|
40
|
|
|
* description="Returned when the entities with given limit and offset are not found", |
|
41
|
|
|
* ) |
|
42
|
|
|
* |
|
43
|
|
|
* @QueryParam(name="limit", requirements="\d+|all", default="all", description="Count entries at one page") |
|
44
|
|
|
* @QueryParam(name="random", requirements="\d+", default=0, description="Should we suffle the order. Use seed in response to keep the same order") |
|
45
|
|
|
* @QueryParam(name="seed", requirements="\d+", default=0, description="Ignored if random is 1") |
|
46
|
|
|
* @QueryParam(name="page", requirements="\d+|middle", default="1", description="Number of page to be shown or 'middle' for middle page") |
|
47
|
|
|
* @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
|
48
|
|
|
*/ |
|
49
|
|
|
public function cgetAction(ParamFetcher $paramFetcher) |
|
50
|
|
|
{ |
|
51
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
52
|
|
|
$page = $paramFetcher->get('page'); |
|
53
|
|
|
$overAllCount = $em->getRepository('App:Employee')->count([]); |
|
54
|
|
|
$limit = $paramFetcher->get('limit', $strict = true) == "all" |
|
55
|
2 |
|
? $overAllCount |
|
56
|
|
|
: $paramFetcher->get('limit'); |
|
57
|
2 |
|
|
|
58
|
|
|
$rand = 0 != $paramFetcher->get('random'); |
|
59
|
|
|
$seed = $paramFetcher->get('seed'); |
|
60
|
2 |
|
if ($rand) { |
|
61
|
2 |
|
$seed = rand(1, 1000); |
|
62
|
|
|
} |
|
63
|
|
|
if ('middle' == $page) { |
|
64
|
2 |
|
$page = round($overAllCount/$limit/2); |
|
65
|
|
|
} |
|
66
|
2 |
|
|
|
67
|
2 |
|
$employeesTranslated = $em->getRepository('App:Employee') |
|
68
|
|
|
->rand($limit, $page, $seed, $paramFetcher->get('locale')); |
|
69
|
2 |
|
|
|
70
|
|
|
$response = new EmployeesResponse(); |
|
71
|
2 |
|
$response->employees = $employeesTranslated; |
|
72
|
2 |
|
$response->currentPage = $page; |
|
73
|
|
|
$response->overAllCount = $overAllCount; |
|
74
|
|
|
$response->seed = $seed; |
|
75
|
2 |
|
$response->rand = $rand; |
|
76
|
2 |
|
|
|
77
|
|
|
return $response; |
|
78
|
2 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
2 |
|
* @Route("/{slug}", name="get_employee", methods={"GET"}) |
|
82
|
|
|
* @SWG\Response( |
|
83
|
2 |
|
* response=200, |
|
84
|
2 |
|
* description="Returns an Employee by unique property {slug}", |
|
85
|
2 |
|
* @Model(type=Employee::class) |
|
86
|
2 |
|
* ) |
|
87
|
2 |
|
* @SWG\Response( |
|
88
|
|
|
* response=404, |
|
89
|
2 |
|
* description="Returns when employee by {slug} not found in database", |
|
90
|
2 |
|
* ) |
|
91
|
2 |
|
* |
|
92
|
2 |
|
* @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
|
93
|
2 |
|
*/ |
|
94
|
|
|
public function getAction(ParamFetcher $paramFetcher, $slug) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
|
97
|
2 |
|
|
|
98
|
2 |
|
/** @var Employee $employee */ |
|
99
|
2 |
|
$employee = $em->getRepository('App:Employee')->findOneByslug($slug); |
|
100
|
|
|
|
|
101
|
|
|
if (!$employee) { |
|
102
|
2 |
|
throw $this->createNotFoundException('Unable to find '.$slug.' entity'); |
|
103
|
2 |
|
} |
|
104
|
2 |
|
|
|
105
|
2 |
|
$employee->setLocale($paramFetcher->get('locale')); |
|
106
|
2 |
|
$em->refresh($employee); |
|
107
|
2 |
|
|
|
108
|
|
|
if ($employee->getTranslations()) { |
|
109
|
2 |
|
$employee->unsetTranslations(); |
|
110
|
|
|
} |
|
111
|
2 |
|
|
|
112
|
|
|
$this->translator->setLocale($paramFetcher->get('locale')); |
|
113
|
|
|
$employee->setPosition($this->translator->trans($employee->getPosition())); |
|
114
|
|
|
|
|
115
|
|
|
return $employee; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
/** |
|
119
|
|
|
* @Route("/{slug}/roles", name="get_employee_roles", methods={"GET"}) |
|
120
|
2 |
|
* @SWG\Response( |
|
121
|
2 |
|
* response=200, |
|
122
|
2 |
|
* description="Returns employee roles by employee {slug}", |
|
123
|
2 |
|
* @SWG\Schema( |
|
124
|
2 |
|
* type="array", |
|
125
|
|
|
* @SWG\Items(ref=@Model(type=Role::class)) |
|
126
|
|
|
* ) |
|
127
|
2 |
|
* ) |
|
128
|
|
|
* @SWG\Response( |
|
129
|
2 |
|
* response=404, |
|
130
|
2 |
|
* description="Returns when employee by {slug} not found in database", |
|
131
|
2 |
|
* ) |
|
132
|
2 |
|
* |
|
133
|
2 |
|
* @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function getRolesAction(ParamFetcher $paramFetcher, $slug) |
|
136
|
|
|
{ |
|
137
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
138
|
|
|
|
|
139
|
|
|
/** @var Employee $employee */ |
|
140
|
|
|
$employee = $em->getRepository('App:Employee')->findOneByslug($slug); |
|
141
|
|
|
|
|
142
|
|
|
if (!$employee) { |
|
143
|
|
|
throw $this->createNotFoundException('Unable to find '.$slug.' entity'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$employee->setLocale($paramFetcher->get('locale')); |
|
147
|
|
|
$em->refresh($employee); |
|
148
|
|
|
|
|
149
|
|
|
$employee->unsetTranslations(); |
|
150
|
|
|
|
|
151
|
|
|
$this->translator->setLocale($paramFetcher->get('locale')); |
|
152
|
1 |
|
$employee->setPosition($this->translator->trans($employee->getPosition())); |
|
153
|
|
|
|
|
154
|
1 |
|
$roles = $employee->getRoles(); |
|
155
|
|
|
|
|
156
|
|
|
$rolesTranslated = []; |
|
157
|
1 |
|
|
|
158
|
|
|
foreach ($roles as $role) { |
|
159
|
1 |
|
/** @var Performance $performance */ |
|
160
|
1 |
|
$performance = $role->getPerformance(); |
|
161
|
|
|
|
|
162
|
|
|
$role->setLocale($paramFetcher->get('locale')); |
|
163
|
1 |
|
$em->refresh($role); |
|
164
|
1 |
|
$performance->setLocale($paramFetcher->get('locale')); |
|
165
|
|
|
$em->refresh($performance); |
|
166
|
1 |
|
|
|
167
|
1 |
|
$role->unsetTranslations(); |
|
168
|
|
|
$performance->unsetTranslations(); |
|
169
|
|
|
|
|
170
|
1 |
|
$rolesTranslated[] = $role; |
|
171
|
1 |
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
$roles = $rolesTranslated; |
|
174
|
|
|
|
|
175
|
|
|
return $roles; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|