1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Users\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Guests\Entity\GuestSession; |
7
|
|
|
use App\Movies\Repository\MovieRepository; |
8
|
|
|
use App\Movies\Request\AddWatchedMovieRequest; |
9
|
|
|
use App\Movies\Request\UpdateWatchedMovieRequest; |
10
|
|
|
use App\Movies\Service\WatchedMovieService; |
11
|
|
|
use App\Pagination\PaginatedCollection; |
12
|
|
|
use App\Users\Entity\User; |
13
|
|
|
use App\Users\Entity\UserWatchedMovie; |
14
|
|
|
use App\Users\Repository\WatchedMovieRepository; |
15
|
|
|
use App\Users\Request\MergeWatchedMoviesRequest; |
16
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
21
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
22
|
|
|
|
23
|
|
|
class WatchedMovieController extends BaseController |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @Route("/api/users/watchedMovies", methods={"POST"}); |
27
|
|
|
* |
28
|
|
|
* @param AddWatchedMovieRequest $addWatchedMovieRequest |
29
|
|
|
* @param Request $request |
30
|
|
|
* @param WatchedMovieService $watchedMovieService |
31
|
|
|
* |
32
|
|
|
* @throws \Exception|NotFoundHttpException |
33
|
|
|
* |
34
|
|
|
* @return JsonResponse |
35
|
|
|
*/ |
36
|
4 |
|
public function postWatchedMovies(AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService) |
37
|
|
|
{ |
38
|
4 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
39
|
|
|
|
40
|
4 |
|
$watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO(); |
41
|
4 |
|
$isMovieAdded = $watchedMovieService->addUserWatchedMovie($this->getUser(), $watchedMovieDTO, $request->getLocale()); |
|
|
|
|
42
|
|
|
|
43
|
4 |
|
if ($isMovieAdded === false) { |
44
|
|
|
throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID'); |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
return new JsonResponse(null, 202); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @Route("/api/users/{user}/watchedMovies/{watchedMovie}", methods={"PATCH"}); |
52
|
|
|
* |
53
|
|
|
* @param UserWatchedMovie $watchedMovie |
54
|
|
|
* @param UpdateWatchedMovieRequest $request |
55
|
|
|
* @param WatchedMovieService $watchedMovieService |
56
|
|
|
* |
57
|
|
|
* @throws \Exception |
58
|
|
|
* |
59
|
|
|
* @return JsonResponse |
60
|
|
|
*/ |
61
|
1 |
|
public function patchWatchedMoviesById(UserWatchedMovie $watchedMovie, UpdateWatchedMovieRequest $request, WatchedMovieService $watchedMovieService) |
62
|
|
|
{ |
63
|
1 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
64
|
|
|
|
65
|
1 |
|
if ($watchedMovie->getUser()->getId() !== $this->getUser()->getId()) { |
66
|
|
|
throw new AccessDeniedHttpException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$watchedMovieDTO = $request->getWatchedMovieDTO(); |
70
|
1 |
|
$watchedMovieService->updateUserWatchedMovie($watchedMovie, $watchedMovieDTO); |
71
|
|
|
|
72
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
73
|
|
|
|
74
|
1 |
|
return new JsonResponse(null, 200); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @Route("/api/users/{user}/watchedMovies/movie/{movieId}", methods={"PATCH"}); |
79
|
|
|
* |
80
|
|
|
* @param int $movieId |
81
|
|
|
* @param UpdateWatchedMovieRequest $request |
82
|
|
|
* @param WatchedMovieService $watchedMovieService |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
* |
86
|
|
|
* @return JsonResponse |
87
|
|
|
*/ |
88
|
1 |
|
public function patchWatchedMoviesByMovieId(int $movieId, UpdateWatchedMovieRequest $request, WatchedMovieService $watchedMovieService, WatchedMovieRepository $repository) |
89
|
|
|
{ |
90
|
1 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
91
|
|
|
|
92
|
|
|
/** @var $user User */ |
93
|
1 |
|
$user = $this->getUser(); |
94
|
|
|
|
95
|
1 |
|
if (null === $watchedMovie = $repository->findOneByMovieId($movieId, $user->getId())) { |
96
|
|
|
throw new NotFoundHttpException(); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$watchedMovieDTO = $request->getWatchedMovieDTO(); |
100
|
1 |
|
$watchedMovieService->updateUserWatchedMovie($watchedMovie, $watchedMovieDTO); |
101
|
|
|
|
102
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
103
|
|
|
|
104
|
1 |
|
return new JsonResponse(null, 200); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @Route("/api/users/{id<\d+>}/watchedMovies", methods={"GET"}); |
109
|
|
|
* |
110
|
|
|
* @param Request $request |
111
|
|
|
* @param User $profileOwner |
112
|
|
|
* @param MovieRepository $repository |
113
|
|
|
* |
114
|
|
|
* @return JsonResponse |
115
|
|
|
*/ |
116
|
|
|
public function getAll(Request $request, User $profileOwner, MovieRepository $repository) |
117
|
|
|
{ |
118
|
|
|
$offset = (int) $request->get('offset', 0); |
119
|
|
|
$limit = $request->get('limit', null); |
120
|
|
|
|
121
|
|
|
$currentUser = $this->getUser(); |
122
|
|
|
|
123
|
|
|
$watchedMovies = new PaginatedCollection( |
124
|
|
|
$repository->getAllWatchedMoviesByUserId($profileOwner->getId(), $currentUser), |
125
|
|
|
$offset, |
126
|
|
|
$limit ? (int) $limit : null |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$groups = ['list']; |
130
|
|
|
|
131
|
|
|
if (!$currentUser || $currentUser->getId() !== $profileOwner->getId()) { |
132
|
|
|
$groups[] = 'userWatchedMovies'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->response($watchedMovies, 200, [], [ |
136
|
|
|
'groups' => $groups, |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @Route("/api/users/{username}/watchedMovies", methods={"GET"}); |
142
|
|
|
* @ParamConverter("user", options={"mapping"={"username"="username"}}) |
143
|
|
|
* |
144
|
|
|
* @param Request $request |
145
|
|
|
* @param User $profileOwner |
146
|
|
|
* @param MovieRepository $repository |
147
|
|
|
* |
148
|
|
|
* @return JsonResponse |
149
|
|
|
*/ |
150
|
2 |
|
public function getAllByUsername(Request $request, User $profileOwner, MovieRepository $repository) |
151
|
|
|
{ |
152
|
2 |
|
$offset = (int) $request->get('offset', 0); |
153
|
2 |
|
$limit = $request->get('limit', null); |
154
|
|
|
|
155
|
2 |
|
$currentUser = $this->getUser(); |
156
|
|
|
|
157
|
2 |
|
$watchedMovies = new PaginatedCollection( |
158
|
2 |
|
$repository->getAllWatchedMoviesByUserId($profileOwner->getId(), $currentUser), |
159
|
2 |
|
$offset, |
160
|
2 |
|
$limit ? (int) $limit : null |
161
|
|
|
); |
162
|
|
|
|
163
|
2 |
|
$groups = ['list']; |
164
|
|
|
|
165
|
2 |
|
if (!$currentUser || $currentUser->getId() !== $profileOwner->getId()) { |
166
|
|
|
$groups[] = 'userWatchedMovies'; |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
return $this->response($watchedMovies, 200, [], [ |
170
|
2 |
|
'groups' => $groups, |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @Route("/api/users/{user}/watchedMovies/{watchedMovieId}", methods={"DELETE"}); |
176
|
|
|
* |
177
|
|
|
* @param int $watchedMovieId |
178
|
|
|
* @param WatchedMovieRepository $repository |
179
|
|
|
* |
180
|
|
|
* @return JsonResponse |
181
|
|
|
*/ |
182
|
|
|
public function deleteWatchedMovies(int $watchedMovieId, WatchedMovieRepository $repository) |
183
|
|
|
{ |
184
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
185
|
|
|
|
186
|
|
|
/** @var $currentUser User */ |
187
|
|
|
$currentUser = $this->getUser(); |
188
|
|
|
|
189
|
|
|
if (null === $watchedMovie = $repository->findOneById($watchedMovieId, $currentUser->getId())) { |
190
|
|
|
$watchedMovie = $repository->findOneByMovieId($watchedMovieId, $currentUser->getId()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (null === $watchedMovie) { |
194
|
|
|
throw new NotFoundHttpException(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$this->getDoctrine()->getManager()->remove($watchedMovie); |
198
|
|
|
$this->getDoctrine()->getManager()->flush(); |
199
|
|
|
|
200
|
|
|
return new JsonResponse(null, 202); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @Route("/api/users/mergeWatchedMovies", methods={"POST"}); |
205
|
|
|
* |
206
|
|
|
* @param MergeWatchedMoviesRequest $mergeWatchedMoviesRequest |
207
|
|
|
* @param WatchedMovieService $watchedMovieService |
208
|
|
|
* |
209
|
|
|
* @throws \Exception |
210
|
|
|
* |
211
|
|
|
* @return JsonResponse |
212
|
|
|
*/ |
213
|
|
|
public function postMergeWatchedMovies(MergeWatchedMoviesRequest $mergeWatchedMoviesRequest, WatchedMovieService $watchedMovieService) |
214
|
|
|
{ |
215
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
216
|
|
|
$guestSessionRepository = $this->getDoctrine()->getRepository(GuestSession::class); |
217
|
|
|
$guestSession = $guestSessionRepository->findOneBy([ |
218
|
|
|
'token' => $mergeWatchedMoviesRequest->get('token'), |
219
|
|
|
]); |
220
|
|
|
|
221
|
|
|
/** @var $guestSession GuestSession|null */ |
222
|
|
|
if ($guestSession === null) { |
223
|
|
|
throw new NotFoundHttpException('Guest session not found by provided token'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$watchedMovieService->mergeWatchedMovies($guestSession, $this->getUser()); |
|
|
|
|
227
|
|
|
|
228
|
|
|
return new JsonResponse(null, 202); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
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: