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