1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Lukasz D. Tulikowski <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace App\Controller; |
13
|
|
|
|
14
|
|
|
use App\Entity\User; |
15
|
|
|
use App\Exception\ApiException; |
16
|
|
|
use App\Form\Filter\UserFilter; |
17
|
|
|
use App\Form\UserType; |
18
|
|
|
use App\Interfaces\ControllerInterface; |
19
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
21
|
|
|
use Swagger\Annotations as SWG; |
22
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
25
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @Route(path="/users") |
29
|
|
|
*/ |
30
|
|
|
class UserController extends AbstractController implements ControllerInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* UserController constructor. |
34
|
|
|
*/ |
35
|
12 |
|
public function __construct() |
36
|
|
|
{ |
37
|
12 |
|
parent::__construct(User::class); |
38
|
12 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get all Users. |
42
|
|
|
* |
43
|
|
|
* @Route(name="api_user_list", methods={Request::METHOD_GET}) |
44
|
|
|
* |
45
|
|
|
* @SWG\Tag(name="User") |
46
|
|
|
* @SWG\Response( |
47
|
|
|
* response=200, |
48
|
|
|
* description="Returns the list of users", |
49
|
|
|
* @SWG\Schema( |
50
|
|
|
* type="array", |
51
|
|
|
* @SWG\Items(ref=@Model(type=User::class)) |
52
|
|
|
* ) |
53
|
|
|
* ) |
54
|
|
|
* |
55
|
|
|
* @param Request $request |
56
|
|
|
* |
57
|
|
|
* @return JsonResponse |
58
|
|
|
*/ |
59
|
2 |
|
public function listAction(Request $request): JsonResponse |
60
|
|
|
{ |
61
|
2 |
|
return $this->createCollectionResponse( |
62
|
2 |
|
$this->handleFilterForm( |
63
|
2 |
|
$request, |
64
|
2 |
|
UserFilter::class |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Show single Users. |
71
|
|
|
* |
72
|
|
|
* @Route(path="/{user}", name="api_user_show", methods={Request::METHOD_GET}) |
73
|
|
|
* |
74
|
|
|
* @SWG\Tag(name="User") |
75
|
|
|
* @SWG\Response( |
76
|
|
|
* response=200, |
77
|
|
|
* description="Returns user of given identifier.", |
78
|
|
|
* @SWG\Schema( |
79
|
|
|
* type="object", |
80
|
|
|
* title="user", |
81
|
|
|
* @SWG\Items(ref=@Model(type=User::class)) |
82
|
|
|
* ) |
83
|
|
|
* ) |
84
|
|
|
* |
85
|
|
|
* @param User|null $user |
86
|
|
|
* |
87
|
|
|
* @return JsonResponse |
88
|
|
|
*/ |
89
|
2 |
|
public function showAction(User $user = null): JsonResponse |
90
|
|
|
{ |
91
|
2 |
|
if (false === !!$user) { |
92
|
1 |
|
return $this->createNotFoundResponse(); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $this->createResourceResponse($user); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add new User. |
100
|
|
|
* |
101
|
|
|
* @Route(name="api_user_create", methods={Request::METHOD_POST}) |
102
|
|
|
* |
103
|
|
|
* @SWG\Tag(name="User") |
104
|
|
|
* @SWG\Response( |
105
|
|
|
* response=200, |
106
|
|
|
* description="Updates User of given identifier and returns the updated object.", |
107
|
|
|
* @SWG\Schema( |
108
|
|
|
* type="object", |
109
|
|
|
* @SWG\Items(ref=@Model(type=User::class)) |
110
|
|
|
* ) |
111
|
|
|
* ) |
112
|
|
|
* |
113
|
|
|
* @param Request $request |
114
|
|
|
* @param User|null $user |
115
|
|
|
* |
116
|
|
|
* @return JsonResponse |
117
|
|
|
*/ |
118
|
2 |
|
public function createAction(Request $request, User $user = null): JsonResponse |
119
|
|
|
{ |
120
|
2 |
|
if (false === !!$user) { |
121
|
2 |
|
$user = new User(); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
$form = $this->getForm( |
125
|
2 |
|
UserType::class, |
126
|
2 |
|
$user, |
127
|
|
|
[ |
128
|
2 |
|
'method' => $request->getMethod(), |
129
|
|
|
] |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
try { |
133
|
2 |
|
$this->formHandler->process($request, $form); |
134
|
1 |
|
} catch (ApiException $e) { |
135
|
1 |
|
return new JsonResponse($e->getData(), Response::HTTP_BAD_REQUEST); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return $this->createResourceResponse($user, Response::HTTP_CREATED); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Edit existing User. |
143
|
|
|
* |
144
|
|
|
* @Route(path="/{user}", name="api_user_edit", methods={Request::METHOD_PATCH}) |
145
|
|
|
* |
146
|
|
|
* @SWG\Tag(name="User") |
147
|
|
|
* @SWG\Response( |
148
|
|
|
* response=200, |
149
|
|
|
* description="Updates User of given identifier and returns the updated object.", |
150
|
|
|
* @SWG\Schema( |
151
|
|
|
* type="object", |
152
|
|
|
* @SWG\Items(ref=@Model(type=User::class)) |
153
|
|
|
* ) |
154
|
|
|
* )* |
155
|
|
|
* |
156
|
|
|
* @param Request $request |
157
|
|
|
* @param User|null $user |
158
|
|
|
* |
159
|
|
|
* @return JsonResponse |
160
|
|
|
* |
161
|
|
|
* @Security("is_granted('CAN_UPDATE_USER', user)") |
162
|
|
|
*/ |
163
|
2 |
|
public function updateAction(Request $request, User $user = null): JsonResponse |
164
|
|
|
{ |
165
|
2 |
|
if (false === !!$user) { |
166
|
1 |
|
return $this->createNotFoundResponse(); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
$form = $this->getForm( |
170
|
1 |
|
UserType::class, |
171
|
1 |
|
$user, |
172
|
|
|
[ |
173
|
1 |
|
'method' => $request->getMethod(), |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
try { |
178
|
1 |
|
$this->formHandler->process($request, $form); |
179
|
|
|
} catch (ApiException $e) { |
180
|
|
|
return new JsonResponse($e->getMessage(), Response::HTTP_BAD_REQUEST); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
return $this->createResourceResponse($user); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Delete User. |
188
|
|
|
* |
189
|
|
|
* @Route(path="/{user}", name="api_user_delete", methods={Request::METHOD_DELETE}) |
190
|
|
|
* |
191
|
|
|
* @SWG\Tag(name="User") |
192
|
|
|
* @SWG\Response( |
193
|
|
|
* response=200, |
194
|
|
|
* description="Delete User of given identifier and returns the empty object.", |
195
|
|
|
* @SWG\Schema( |
196
|
|
|
* type="object", |
197
|
|
|
* @SWG\Items(ref=@Model(type=User::class)) |
198
|
|
|
* ) |
199
|
|
|
* ) |
200
|
|
|
* |
201
|
|
|
* @param User $user |
202
|
|
|
* |
203
|
|
|
* @return JsonResponse |
204
|
|
|
* |
205
|
|
|
* @Security("is_granted('CAN_DELETE_USER', user)") |
206
|
|
|
*/ |
207
|
2 |
|
public function deleteAction(User $user = null): JsonResponse |
208
|
|
|
{ |
209
|
2 |
|
if (false === !!$user) { |
210
|
1 |
|
return $this->createNotFoundResponse(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
try { |
214
|
1 |
|
$this->entityManager->remove($user); |
215
|
1 |
|
$this->entityManager->flush(); |
216
|
|
|
} catch (\Exception $exception) { |
217
|
|
|
return $this->createGenericErrorResponse($exception); |
218
|
|
|
} |
219
|
|
|
|
220
|
1 |
|
return $this->createSuccessfulApiResponse(self::DELETED); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|