1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Movies\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Movies\Entity\Movie; |
7
|
|
|
use App\Movies\Entity\MovieCard; |
8
|
|
|
use App\Movies\Repository\MovieCardRepository; |
9
|
|
|
use App\Movies\Request\NewMovieCardRequest; |
10
|
|
|
use App\Pagination\PaginatedCollection; |
11
|
|
|
use App\Users\Entity\UserRoles; |
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
16
|
|
|
|
17
|
|
|
class MovieCardController extends BaseController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @Route("/api/movies/{id}/cards", methods={"GET"}, requirements={"id"="\d+"}) |
21
|
|
|
* |
22
|
|
|
* @param Request $request |
23
|
|
|
* @param int $id |
24
|
|
|
* @param MovieCardRepository $repository |
25
|
|
|
* |
26
|
|
|
* @return JsonResponse |
27
|
|
|
*/ |
28
|
1 |
|
public function getMoviesCards(Request $request, int $id, MovieCardRepository $repository) |
29
|
|
|
{ |
30
|
1 |
|
$cards = $repository->findAllByMovie($request->getLocale(), $id); |
31
|
|
|
|
32
|
1 |
|
$offset = (int) $request->get('offset', 0); |
33
|
1 |
|
$limit = $request->get('limit', null); |
34
|
|
|
|
35
|
1 |
|
$collection = new PaginatedCollection($cards, $offset, $limit); |
36
|
|
|
|
37
|
1 |
|
return $this->response($collection, 200, [], [ |
38
|
1 |
|
'groups' => ['list'], |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add new recommendation. |
44
|
|
|
* |
45
|
|
|
* @Route("/api/movies/{id}/cards", methods={"POST"}) |
46
|
|
|
* |
47
|
|
|
* @throws \Doctrine\ORM\ORMException |
48
|
|
|
* |
49
|
|
|
* @return JsonResponse |
50
|
|
|
*/ |
51
|
1 |
|
public function postMoviesCards(Request $request, NewMovieCardRequest $cardRequest, Movie $movie, EntityManagerInterface $em) |
52
|
|
|
{ |
53
|
1 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_USER); |
54
|
|
|
|
55
|
1 |
|
$card = $cardRequest->get('card'); |
56
|
1 |
|
$user = $this->getUser(); |
57
|
|
|
|
58
|
1 |
|
$card = new MovieCard( |
59
|
1 |
|
$movie, |
60
|
1 |
|
$user, |
|
|
|
|
61
|
1 |
|
$request->getLocale(), |
62
|
1 |
|
$card['title'], |
63
|
1 |
|
$card['description'], |
64
|
1 |
|
$card['type'], |
65
|
1 |
|
$card['url'] |
66
|
|
|
); |
67
|
|
|
|
68
|
1 |
|
$em->persist($card); |
69
|
1 |
|
$em->flush(); |
70
|
|
|
|
71
|
1 |
|
return new JsonResponse(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @Route("/api/movies/{movie_id}/cards/{id}", methods={"DELETE"}) |
76
|
|
|
*/ |
77
|
1 |
|
public function deleteMoviesCards(MovieCard $card, EntityManagerInterface $em) |
78
|
|
|
{ |
79
|
1 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_USER); |
80
|
|
|
|
81
|
1 |
|
if ($card->getUser()->getId() !== $this->getUser()->getId()) { |
82
|
|
|
$this->denyAccessUnlessGranted([UserRoles::ROLE_MODERATOR, UserRoles::ROLE_ADMIN]); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$em->remove($card); |
86
|
1 |
|
$em->flush(); |
87
|
|
|
|
88
|
1 |
|
return new JsonResponse(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: