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\MovieReview; |
8
|
|
|
use App\Movies\Repository\MovieReviewRepository; |
9
|
|
|
use App\Movies\Request\NewMovieReviewRequest; |
10
|
|
|
use App\Pagination\PaginatedCollection; |
11
|
|
|
use App\Users\Entity\UserRoles; |
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
18
|
|
|
|
19
|
|
|
class MovieReviewController extends BaseController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/api/movies/{id}/reviews", methods={"GET"}, requirements={"id"="\d+"}) |
23
|
|
|
*/ |
24
|
1 |
|
public function getMoviesReviews(Request $request, int $id, MovieReviewRepository $repository) |
25
|
|
|
{ |
26
|
1 |
|
$reviews = $repository->findAllByMovie($request->getLocale(), $id); |
27
|
|
|
|
28
|
1 |
|
$offset = (int) $request->get('offset', 0); |
29
|
1 |
|
$limit = $request->get('limit', null); |
30
|
|
|
|
31
|
1 |
|
$collection = new PaginatedCollection($reviews, $offset, $limit); |
32
|
|
|
|
33
|
1 |
|
return $this->response($collection, 200, [], [ |
34
|
1 |
|
'groups' => ['list'], |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @Route("/api/movies/{id}/reviews", methods={"POST"}, requirements={"id"="\d+"}) |
40
|
|
|
*/ |
41
|
1 |
|
public function postMoviesReviews(Request $request, NewMovieReviewRequest $reviewRequest, Movie $movie, EntityManagerInterface $em) |
42
|
|
|
{ |
43
|
1 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_USER); |
44
|
|
|
|
45
|
1 |
|
$review = $reviewRequest->get('review'); |
46
|
1 |
|
$user = $this->getUser(); |
47
|
|
|
|
48
|
1 |
|
$review = new MovieReview( |
49
|
1 |
|
$movie, |
50
|
1 |
|
$user, |
|
|
|
|
51
|
1 |
|
$request->getLocale(), |
52
|
1 |
|
$review['text'] |
53
|
|
|
); |
54
|
|
|
|
55
|
1 |
|
$em->persist($review); |
56
|
1 |
|
$em->flush(); |
57
|
|
|
|
58
|
1 |
|
return new JsonResponse(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @Route("/api/movies/{movie_id}/reviews/{id}", methods={"DELETE"}) |
63
|
|
|
*/ |
64
|
1 |
|
public function deleteMoviesReviews(int $id, EntityManagerInterface $em, MovieReviewRepository $repository) |
65
|
|
|
{ |
66
|
1 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_USER); |
67
|
|
|
|
68
|
1 |
|
if (null === $review = $repository->findOne($id)) { |
69
|
|
|
throw new NotFoundHttpException(); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
if ($review->getUser()->getId() !== $this->getUser()->getId()) { |
73
|
|
|
$this->denyAccessUnlessGranted([UserRoles::ROLE_MODERATOR, UserRoles::ROLE_ADMIN]); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$em->remove($review); |
77
|
1 |
|
$em->flush(); |
78
|
|
|
|
79
|
1 |
|
return new JsonResponse(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: