1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Movies\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Movies\Entity\Movie; |
7
|
|
|
use App\Movies\EventListener\AddRecommendationProcessor; |
8
|
|
|
use App\Movies\Repository\MovieRepository; |
9
|
|
|
use App\Movies\Request\CreateMovieRequest; |
10
|
|
|
use App\Movies\Request\NewMovieRecommendationRequest; |
11
|
|
|
use App\Movies\Request\SearchRequest; |
12
|
|
|
use App\Movies\Service\MovieManageService; |
13
|
|
|
use App\Movies\Service\SearchService; |
14
|
|
|
use App\Pagination\PaginatedCollection; |
15
|
|
|
use App\Users\Entity\User; |
16
|
|
|
use App\Users\Entity\UserRoles; |
17
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Enqueue\Client\ProducerInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
23
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
24
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class MovieController. |
28
|
|
|
*/ |
29
|
|
|
class MovieController extends BaseController |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Get all movies. |
33
|
|
|
* |
34
|
|
|
* @Route("/api/movies", methods={"GET"}) |
35
|
|
|
* |
36
|
|
|
* @param Request $request |
37
|
|
|
* @param MovieRepository $movieRepository |
38
|
|
|
* |
39
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
40
|
|
|
*/ |
41
|
4 |
|
public function getAll(Request $request, MovieRepository $movieRepository) |
42
|
|
|
{ |
43
|
4 |
|
$user = $this->getUser(); |
44
|
|
|
|
45
|
4 |
|
if ($user instanceof User) { |
46
|
|
|
$movies = $movieRepository->findAllWithIsUserWatchedFlag($user); |
47
|
|
|
} else { |
48
|
4 |
|
$movies = $movieRepository->findAllWithIsGuestWatchedFlag($this->getGuest()); |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
$offset = (int) $request->get('offset', 0); |
52
|
4 |
|
$limit = $request->get('limit', null); |
53
|
|
|
|
54
|
4 |
|
$movies = new PaginatedCollection($movies, $offset, $limit); |
55
|
|
|
|
56
|
4 |
|
return $this->response($movies, 200, [], [ |
57
|
4 |
|
'groups' => ['list'], |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get movie resource. |
63
|
|
|
* |
64
|
|
|
* @Route("/api/movies/{id}", methods={"GET"}) |
65
|
|
|
* |
66
|
|
|
* @param Movie $movie |
67
|
|
|
* |
68
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
69
|
|
|
*/ |
70
|
|
|
public function getMovies(Movie $movie) |
71
|
|
|
{ |
72
|
|
|
return $this->response($movie, 200, [], [ |
73
|
|
|
'groups' => ['view'], |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get movies by title. |
79
|
|
|
* |
80
|
|
|
* @Route("/api/movies/search", methods={"POST"}) |
81
|
|
|
* |
82
|
|
|
* @param SearchRequest $request |
83
|
|
|
* @param SearchService $searchService |
84
|
|
|
* @param Request $currentRequest |
85
|
|
|
* |
86
|
|
|
* @throws \Exception |
87
|
|
|
* |
88
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
89
|
|
|
*/ |
90
|
2 |
|
public function getSearch(SearchRequest $request, SearchService $searchService, Request $currentRequest) |
91
|
|
|
{ |
92
|
2 |
|
$offset = (int) $request->get('offset', 0); |
93
|
2 |
|
$limit = $request->get('limit', null); |
94
|
|
|
|
95
|
2 |
|
$query = $request->get('query'); |
96
|
2 |
|
$movies = $searchService->findByQuery($query, $currentRequest->getLocale(), $offset, $limit); |
97
|
|
|
|
98
|
2 |
|
return $this->response($movies, 200, [], [ |
99
|
2 |
|
'groups' => ['list'], |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create new movie. |
105
|
|
|
* |
106
|
|
|
* @Route("/api/movies", methods={"POST"}) |
107
|
|
|
* |
108
|
|
|
* @param CreateMovieRequest $request |
109
|
|
|
* @param MovieManageService $service |
110
|
|
|
* @param ValidatorInterface $validator |
111
|
|
|
* |
112
|
|
|
* @throws \Exception |
113
|
|
|
* |
114
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
115
|
|
|
*/ |
116
|
2 |
|
public function postMovies(CreateMovieRequest $request, MovieManageService $service, ValidatorInterface $validator) |
117
|
|
|
{ |
118
|
2 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN); |
119
|
|
|
|
120
|
1 |
|
$movie = $service->createMovieByRequest($request); |
121
|
1 |
|
$errors = $validator->validate($movie); |
122
|
|
|
|
123
|
1 |
|
if (count($errors)) { |
124
|
|
|
return $request->getErrorResponse($errors); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
128
|
1 |
|
$entityManager->persist($movie); |
129
|
1 |
|
$entityManager->flush(); |
130
|
|
|
|
131
|
1 |
|
return $this->response($movie, 200, [], [ |
132
|
1 |
|
'groups' => ['view'], |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add new recommendation |
138
|
|
|
* |
139
|
|
|
* @Route("/api/movies/{id}/recommendations", methods={"POST"}) |
140
|
|
|
|
141
|
|
|
* @param NewMovieRecommendationRequest $request |
142
|
|
|
* @param Movie $originalMovie |
143
|
|
|
* @param EntityManagerInterface $em |
144
|
|
|
* @param ProducerInterface $producer |
145
|
|
|
* @return JsonResponse |
146
|
|
|
* @throws \Doctrine\ORM\ORMException |
147
|
|
|
*/ |
148
|
1 |
|
public function postMoviesRecommendations(NewMovieRecommendationRequest $request, Movie $originalMovie, EntityManagerInterface $em, ProducerInterface $producer) |
149
|
|
|
{ |
150
|
1 |
|
$this->denyAccessUnlessGranted(UserRoles::ROLE_USER); |
151
|
|
|
|
152
|
1 |
|
$recommendation = $request->get('recommendation'); |
153
|
1 |
|
$user = $this->getUser(); |
154
|
|
|
|
155
|
1 |
|
if (!empty($recommendation['movie_id'])) { |
156
|
1 |
|
$recommendedMovie = $em->getReference(Movie::class, $recommendation['movie_id']); |
157
|
|
|
|
158
|
1 |
|
if ($recommendedMovie === null) { |
159
|
|
|
throw new NotFoundHttpException(); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
$originalMovie->addRecommendation($user, $recommendedMovie); |
|
|
|
|
163
|
1 |
|
$em->persist($originalMovie); |
164
|
|
|
try { |
165
|
1 |
|
$em->flush(); |
166
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
167
|
|
|
// It's ok.. |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
return new JsonResponse(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$producer->sendEvent(AddRecommendationProcessor::ADD_RECOMMENDATION, json_encode([ |
174
|
|
|
'tmdb_id' => $recommendation['tmdb_id'], |
175
|
|
|
'movie_id' => $originalMovie->getId(), |
176
|
|
|
'user_id' => $user->getId(), |
177
|
|
|
])); |
178
|
|
|
|
179
|
|
|
return new JsonResponse(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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: