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