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\Review; |
15
|
|
|
use App\Exception\ApiException; |
16
|
|
|
use App\Form\Filter\ReviewFilter; |
17
|
|
|
use App\Form\ReviewType; |
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="/reviews") |
29
|
|
|
*/ |
30
|
|
|
class ReviewController extends AbstractController implements ControllerInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* ReviewController constructor. |
34
|
|
|
*/ |
35
|
13 |
|
public function __construct() |
36
|
|
|
{ |
37
|
13 |
|
parent::__construct(Review::class); |
38
|
13 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get all Reviews. |
42
|
|
|
* |
43
|
|
|
* @Route(name="api_review_list", methods={Request::METHOD_GET}) |
44
|
|
|
* |
45
|
|
|
* @SWG\Tag(name="Review") |
46
|
|
|
* @SWG\Response( |
47
|
|
|
* response=200, |
48
|
|
|
* description="Returns the list of reviews", |
49
|
|
|
* @SWG\Schema( |
50
|
|
|
* type="array", |
51
|
|
|
* @SWG\Items(ref=@Model(type=Review::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 |
|
ReviewFilter::class |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Show single Reviews. |
71
|
|
|
* |
72
|
|
|
* @Route(path="/{review}", name="api_review_show", methods={Request::METHOD_GET}) |
73
|
|
|
* |
74
|
|
|
* @SWG\Tag(name="Review") |
75
|
|
|
* @SWG\Response( |
76
|
|
|
* response=200, |
77
|
|
|
* description="Returns review of given identifier.", |
78
|
|
|
* @SWG\Schema( |
79
|
|
|
* type="object", |
80
|
|
|
* title="review", |
81
|
|
|
* @SWG\Items(ref=@Model(type=Review::class)) |
82
|
|
|
* ) |
83
|
|
|
* ) |
84
|
|
|
* |
85
|
|
|
* @param Review|null $review |
86
|
|
|
* |
87
|
|
|
* @return JsonResponse |
88
|
|
|
*/ |
89
|
2 |
|
public function showAction(Review $review = null): JsonResponse |
90
|
|
|
{ |
91
|
2 |
|
if (false === !!$review) { |
92
|
1 |
|
return $this->createNotFoundResponse(); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $this->createResourceResponse($review); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add new Review. |
100
|
|
|
* |
101
|
|
|
* @Route(name="api_review_create", methods={Request::METHOD_POST}) |
102
|
|
|
* |
103
|
|
|
* @SWG\Tag(name="Review") |
104
|
|
|
* @SWG\Response( |
105
|
|
|
* response=200, |
106
|
|
|
* description="Updates Review of given identifier and returns the updated object.", |
107
|
|
|
* @SWG\Schema( |
108
|
|
|
* type="object", |
109
|
|
|
* @SWG\Items(ref=@Model(type=Review::class)) |
110
|
|
|
* ) |
111
|
|
|
* ) |
112
|
|
|
* |
113
|
|
|
* @param Request $request |
114
|
|
|
* @param Review $review |
115
|
|
|
* |
116
|
|
|
* @return JsonResponse |
117
|
|
|
* |
118
|
|
|
* @Security("is_granted('CAN_CREATE_REVIEW', review)") |
119
|
|
|
*/ |
120
|
2 |
|
public function createAction(Request $request, Review $review = null): JsonResponse |
121
|
|
|
{ |
122
|
2 |
|
if (false === !!$review) { |
123
|
2 |
|
$review = new Review(); |
124
|
2 |
|
$review->setAuthor($this->getUser()); |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
$form = $this->getForm( |
128
|
2 |
|
ReviewType::class, |
129
|
2 |
|
$review, |
130
|
|
|
[ |
131
|
2 |
|
'method' => $request->getMethod(), |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
try { |
136
|
2 |
|
$this->formHandler->process($request, $form); |
137
|
1 |
|
} catch (ApiException $e) { |
138
|
1 |
|
return new JsonResponse($e->getData(), Response::HTTP_BAD_REQUEST); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
return $this->createResourceResponse($review, Response::HTTP_CREATED); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Edit existing Review. |
146
|
|
|
* |
147
|
|
|
* @Route(path="/{review}", name="api_review_edit", methods={Request::METHOD_PATCH}) |
148
|
|
|
* |
149
|
|
|
* @SWG\Tag(name="Review") |
150
|
|
|
* @SWG\Response( |
151
|
|
|
* response=200, |
152
|
|
|
* description="Updates Review of given identifier and returns the updated object.", |
153
|
|
|
* @SWG\Schema( |
154
|
|
|
* type="object", |
155
|
|
|
* @SWG\Items(ref=@Model(type=Review::class)) |
156
|
|
|
* ) |
157
|
|
|
* ) |
158
|
|
|
* |
159
|
|
|
* @param Request $request |
160
|
|
|
* @param Review|null $review |
161
|
|
|
* |
162
|
|
|
* @return JsonResponse |
163
|
|
|
* |
164
|
|
|
* @Security("is_granted('CAN_UPDATE_REVIEW', review)") |
165
|
|
|
*/ |
166
|
2 |
|
public function updateAction(Request $request, Review $review = null): JsonResponse |
167
|
|
|
{ |
168
|
2 |
|
if (false === !!$review) { |
169
|
1 |
|
return $this->createNotFoundResponse(); |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
$form = $this->getForm( |
173
|
1 |
|
ReviewType::class, |
174
|
1 |
|
$review, |
175
|
|
|
[ |
176
|
1 |
|
'method' => $request->getMethod(), |
177
|
|
|
] |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
try { |
181
|
1 |
|
$this->formHandler->process($request, $form); |
182
|
|
|
} catch (ApiException $e) { |
183
|
|
|
return new JsonResponse($e->getMessage(), Response::HTTP_BAD_REQUEST); |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
return $this->createResourceResponse($review); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Delete Review. |
191
|
|
|
* |
192
|
|
|
* @Route(path="/{review}", name="api_review_delete", methods={Request::METHOD_DELETE}) |
193
|
|
|
* |
194
|
|
|
* @SWG\Tag(name="Review") |
195
|
|
|
* @SWG\Response( |
196
|
|
|
* response=200, |
197
|
|
|
* description="Delete Review of given identifier and returns the empty object.", |
198
|
|
|
* @SWG\Schema( |
199
|
|
|
* type="object", |
200
|
|
|
* @SWG\Items(ref=@Model(type=Review::class)) |
201
|
|
|
* ) |
202
|
|
|
* ) |
203
|
|
|
* |
204
|
|
|
* @param Review|null $review |
205
|
|
|
* |
206
|
|
|
* @return JsonResponse |
207
|
|
|
* |
208
|
|
|
* @Security("is_granted('CAN_DELETE_REVIEW', review)") |
209
|
|
|
*/ |
210
|
2 |
|
public function deleteAction(Review $review = null): JsonResponse |
211
|
|
|
{ |
212
|
2 |
|
if (false === !!$review) { |
213
|
1 |
|
return $this->createNotFoundResponse(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
try { |
217
|
1 |
|
$this->entityManager->remove($review); |
218
|
1 |
|
$this->entityManager->flush(); |
219
|
|
|
} catch (\Exception $exception) { |
220
|
|
|
return $this->createGenericErrorResponse($exception); |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
return $this->createSuccessfulApiResponse(self::DELETED); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|