1 | <?php |
||
39 | class GalleryController |
||
40 | { |
||
41 | /** |
||
42 | * @var GalleryManagerInterface |
||
43 | */ |
||
44 | protected $galleryManager; |
||
45 | |||
46 | /** |
||
47 | * @var MediaManagerInterface |
||
48 | */ |
||
49 | protected $mediaManager; |
||
50 | |||
51 | /** |
||
52 | * @var FormFactoryInterface |
||
53 | */ |
||
54 | protected $formFactory; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $galleryItemClass; |
||
60 | |||
61 | /** |
||
62 | * @param string $galleryItemClass |
||
63 | */ |
||
64 | public function __construct(GalleryManagerInterface $galleryManager, MediaManagerInterface $mediaManager, FormFactoryInterface $formFactory, $galleryItemClass) |
||
71 | |||
72 | /** |
||
73 | * Retrieves the list of galleries (paginated). |
||
74 | * |
||
75 | * @ApiDoc( |
||
76 | * resource=true, |
||
77 | * output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}} |
||
78 | * ) |
||
79 | * |
||
80 | * @Rest\QueryParam( |
||
81 | * name="page", |
||
82 | * requirements="\d+", |
||
83 | * default="1", |
||
84 | * description="Page for gallery list pagination" |
||
85 | * ) |
||
86 | * @Rest\QueryParam( |
||
87 | * name="count", |
||
88 | * requirements="\d+", |
||
89 | * default="10", |
||
90 | * description="Number of galleries by page" |
||
91 | * ) |
||
92 | * @Rest\QueryParam( |
||
93 | * name="enabled", |
||
94 | * requirements="0|1", |
||
95 | * nullable=true, |
||
96 | * strict=true, |
||
97 | * description="Enabled/Disabled galleries filter" |
||
98 | * ) |
||
99 | * @Rest\QueryParam( |
||
100 | * name="orderBy", |
||
101 | * map=true, |
||
102 | * requirements="ASC|DESC", |
||
103 | * nullable=true, |
||
104 | * strict=true, |
||
105 | * description="Order by array (key is field, value is direction)" |
||
106 | * ) |
||
107 | * |
||
108 | * @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
109 | * |
||
110 | * @return PagerInterface |
||
111 | */ |
||
112 | public function getGalleriesAction(ParamFetcherInterface $paramFetcher) |
||
137 | |||
138 | /** |
||
139 | * Retrieves a specific gallery. |
||
140 | * |
||
141 | * @ApiDoc( |
||
142 | * requirements={ |
||
143 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"} |
||
144 | * }, |
||
145 | * output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
||
146 | * statusCodes={ |
||
147 | * 200="Returned when successful", |
||
148 | * 404="Returned when gallery is not found" |
||
149 | * } |
||
150 | * ) |
||
151 | * |
||
152 | * @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
153 | * |
||
154 | * @param $id |
||
155 | * |
||
156 | * @return GalleryInterface |
||
157 | */ |
||
158 | public function getGalleryAction($id) |
||
162 | |||
163 | /** |
||
164 | * Retrieves the medias of specified gallery. |
||
165 | * |
||
166 | * @ApiDoc( |
||
167 | * requirements={ |
||
168 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"} |
||
169 | * }, |
||
170 | * output={"class"="Sonata\MediaBundle\Model\Media", "groups"={"sonata_api_read"}}, |
||
171 | * statusCodes={ |
||
172 | * 200="Returned when successful", |
||
173 | * 404="Returned when gallery is not found" |
||
174 | * } |
||
175 | * ) |
||
176 | * |
||
177 | * @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
178 | * |
||
179 | * @param $id |
||
180 | * |
||
181 | * @return MediaInterface[] |
||
182 | */ |
||
183 | public function getGalleryMediasAction($id) |
||
194 | |||
195 | /** |
||
196 | * Retrieves the gallery items of specified gallery. |
||
197 | * |
||
198 | * @ApiDoc( |
||
199 | * requirements={ |
||
200 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"} |
||
201 | * }, |
||
202 | * output={"class"="Sonata\MediaBundle\Model\GalleryItem", "groups"={"sonata_api_read"}}, |
||
203 | * statusCodes={ |
||
204 | * 200="Returned when successful", |
||
205 | * 404="Returned when gallery is not found" |
||
206 | * } |
||
207 | * ) |
||
208 | * |
||
209 | * @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
210 | * |
||
211 | * @param $id |
||
212 | * |
||
213 | * @return GalleryItemInterface[] |
||
214 | */ |
||
215 | public function getGalleryGalleryItemsAction($id) |
||
216 | { |
||
217 | return $this->getGallery($id)->getGalleryItems(); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Adds a gallery. |
||
222 | * |
||
223 | * @ApiDoc( |
||
224 | * input={"class"="sonata_media_api_form_gallery", "name"="", "groups"={"sonata_api_write"}}, |
||
225 | * output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
||
226 | * statusCodes={ |
||
227 | * 200="Returned when successful", |
||
228 | * 400="Returned when an error has occurred while gallery creation", |
||
229 | * } |
||
230 | * ) |
||
231 | * |
||
232 | * @param Request $request Symfony request |
||
233 | * |
||
234 | * @throws NotFoundHttpException |
||
235 | * |
||
236 | * @return GalleryInterface |
||
237 | */ |
||
238 | public function postGalleryAction(Request $request) |
||
242 | |||
243 | /** |
||
244 | * Updates a gallery. |
||
245 | * |
||
246 | * @ApiDoc( |
||
247 | * requirements={ |
||
248 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"} |
||
249 | * }, |
||
250 | * input={"class"="sonata_media_api_form_gallery", "name"="", "groups"={"sonata_api_write"}}, |
||
251 | * output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
||
252 | * statusCodes={ |
||
253 | * 200="Returned when successful", |
||
254 | * 400="Returned when an error has occurred while gallery creation", |
||
255 | * 404="Returned when unable to find gallery" |
||
256 | * } |
||
257 | * ) |
||
258 | * |
||
259 | * @param int $id User identifier |
||
260 | * @param Request $request Symfony request |
||
261 | * |
||
262 | * @throws NotFoundHttpException |
||
263 | * |
||
264 | * @return GalleryInterface |
||
265 | */ |
||
266 | public function putGalleryAction($id, Request $request) |
||
270 | |||
271 | /** |
||
272 | * Adds a medium to a gallery. |
||
273 | * |
||
274 | * @ApiDoc( |
||
275 | * requirements={ |
||
276 | * {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"}, |
||
277 | * {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="Medium identifier"} |
||
278 | * }, |
||
279 | * input={"class"="sonata_media_api_form_gallery_item", "name"="", "groups"={"sonata_api_write"}}, |
||
280 | * output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
||
281 | * statusCodes={ |
||
282 | * 200="Returned when successful", |
||
283 | * 400="Returned when an error has occurred while gallery/media attachment", |
||
284 | * } |
||
285 | * ) |
||
286 | * |
||
287 | * @param int $galleryId Gallery identifier |
||
288 | * @param int $mediaId Medium identifier |
||
289 | * @param Request $request Symfony request |
||
290 | * |
||
291 | * @throws NotFoundHttpException |
||
292 | * |
||
293 | * @return GalleryInterface |
||
294 | */ |
||
295 | public function postGalleryMediaGalleryItemAction($galleryId, $mediaId, Request $request) |
||
310 | |||
311 | /** |
||
312 | * Updates a medium to a gallery. |
||
313 | * |
||
314 | * @ApiDoc( |
||
315 | * requirements={ |
||
316 | * {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"}, |
||
317 | * {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="Medium identifier"} |
||
318 | * }, |
||
319 | * input={"class"="sonata_media_api_form_gallery_item", "name"="", "groups"={"sonata_api_write"}}, |
||
320 | * output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
||
321 | * statusCodes={ |
||
322 | * 200="Returned when successful", |
||
323 | * 404="Returned when an error if medium cannot be found in gallery", |
||
324 | * } |
||
325 | * ) |
||
326 | * |
||
327 | * @param int $galleryId Gallery identifier |
||
328 | * @param int $mediaId Medium identifier |
||
329 | * @param Request $request Symfony request |
||
330 | * |
||
331 | * @throws NotFoundHttpException |
||
332 | * |
||
333 | * @return GalleryInterface |
||
334 | */ |
||
335 | public function putGalleryMediaGalleryItemAction($galleryId, $mediaId, Request $request) |
||
348 | |||
349 | /** |
||
350 | * Deletes a medium association to a gallery. |
||
351 | * |
||
352 | * @ApiDoc( |
||
353 | * requirements={ |
||
354 | * {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"}, |
||
355 | * {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="Medium identifier"} |
||
356 | * }, |
||
357 | * statusCodes={ |
||
358 | * 200="Returned when medium is successfully deleted from gallery", |
||
359 | * 400="Returned when an error has occurred while medium deletion of gallery", |
||
360 | * 404="Returned when unable to find gallery or media" |
||
361 | * } |
||
362 | * ) |
||
363 | * |
||
364 | * @param int $galleryId Gallery identifier |
||
365 | * @param int $mediaId Media identifier |
||
366 | * |
||
367 | * @throws NotFoundHttpException |
||
368 | * |
||
369 | * @return Rest\View |
||
370 | */ |
||
371 | public function deleteGalleryMediaGalleryItemAction($galleryId, $mediaId) |
||
389 | |||
390 | /** |
||
391 | * Deletes a gallery. |
||
392 | * |
||
393 | * @ApiDoc( |
||
394 | * requirements={ |
||
395 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Gallery identifier"} |
||
396 | * }, |
||
397 | * statusCodes={ |
||
398 | * 200="Returned when gallery is successfully deleted", |
||
399 | * 400="Returned when an error has occurred while gallery deletion", |
||
400 | * 404="Returned when unable to find gallery" |
||
401 | * } |
||
402 | * ) |
||
403 | * |
||
404 | * @param int $id Gallery identifier |
||
405 | * |
||
406 | * @throws NotFoundHttpException |
||
407 | * |
||
408 | * @return Rest\View |
||
409 | */ |
||
410 | public function deleteGalleryAction($id) |
||
418 | |||
419 | /** |
||
420 | * Write a GalleryItem, this method is used by both POST and PUT action methods. |
||
421 | * |
||
422 | * @return FormInterface |
||
423 | */ |
||
424 | protected function handleWriteGalleryItem(GalleryInterface $gallery, MediaInterface $media, ?GalleryItemInterface $galleryItem = null, Request $request) |
||
452 | |||
453 | /** |
||
454 | * Retrieves gallery with identifier $id or throws an exception if it doesn't exist. |
||
455 | * |
||
456 | * @throws NotFoundHttpException |
||
457 | * |
||
458 | * @return GalleryInterface |
||
459 | */ |
||
460 | protected function getGallery($id) |
||
470 | |||
471 | /** |
||
472 | * Retrieves media with identifier $id or throws an exception if it doesn't exist. |
||
473 | * |
||
474 | * @throws NotFoundHttpException |
||
475 | * |
||
476 | * @return MediaInterface |
||
477 | */ |
||
478 | protected function getMedia($id) |
||
488 | |||
489 | /** |
||
490 | * @return GalleryManagerInterface |
||
491 | */ |
||
492 | protected function getGalleryManager() |
||
496 | |||
497 | /** |
||
498 | * @return MediaManagerInterface |
||
499 | */ |
||
500 | protected function getMediaManager() |
||
504 | |||
505 | /** |
||
506 | * Write a Gallery, this method is used by both POST and PUT action methods. |
||
507 | * |
||
508 | * @param Request $request Symfony request |
||
509 | * @param int|null $id Gallery identifier |
||
510 | * |
||
511 | * @return Rest\View|FormInterface |
||
512 | */ |
||
513 | protected function handleWriteGallery($request, $id = null) |
||
539 | } |
||
540 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.