1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Users\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Movies\Entity\Movie; |
7
|
|
|
use App\Movies\Repository\MovieRepository; |
8
|
|
|
use App\Pagination\PaginatedCollection; |
9
|
|
|
use App\Users\Entity\User; |
10
|
|
|
use App\Users\Entity\UserInterestedMovie; |
11
|
|
|
use App\Users\Repository\InterestedMovieRepository; |
12
|
|
|
use App\Users\Request\AddInterestedMovieRequest; |
13
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
18
|
|
|
|
19
|
|
|
class InterestedMovieController extends BaseController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/api/users/{id<\d+>}/interestedMovies", methods={"GET"}); |
23
|
|
|
* |
24
|
|
|
* @param Request $request |
25
|
|
|
* @param User $user |
26
|
|
|
* @param MovieRepository $repository |
27
|
|
|
* |
28
|
|
|
* @return JsonResponse |
29
|
|
|
*/ |
30
|
3 |
|
public function getAll(Request $request, User $user, MovieRepository $repository) |
31
|
|
|
{ |
32
|
3 |
|
$offset = (int) $request->get('offset', 0); |
33
|
3 |
|
$limit = $request->get('limit', null); |
34
|
|
|
|
35
|
3 |
|
$interestedMovies = new PaginatedCollection( |
36
|
3 |
|
$repository->getAllInterestedMoviesByUserId($user->getId(), $this->getUser()), |
37
|
3 |
|
$offset, |
38
|
3 |
|
$limit ? (int) $limit : null |
39
|
|
|
); |
40
|
|
|
|
41
|
3 |
|
return $this->response($interestedMovies, 200, [], [ |
42
|
3 |
|
'groups' => ['list'], |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @Route("/api/users/interestedMovies", methods={"POST"}); |
48
|
|
|
* |
49
|
|
|
* @param AddInterestedMovieRequest $request |
50
|
|
|
* @param EntityManagerInterface $em |
51
|
|
|
* |
52
|
|
|
* @throws \Exception |
53
|
|
|
* |
54
|
|
|
* @return JsonResponse |
55
|
|
|
*/ |
56
|
3 |
|
public function postInterestedMovies(AddInterestedMovieRequest $request, EntityManagerInterface $em) |
57
|
|
|
{ |
58
|
3 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
59
|
|
|
|
60
|
|
|
/** @var $movie Movie */ |
61
|
3 |
|
$movie = $em->getReference(Movie::class, $request->get('movie_id')); |
62
|
3 |
|
$interestedMovie = new UserInterestedMovie($this->getUser(), $movie); |
|
|
|
|
63
|
3 |
|
$em->persist($interestedMovie); |
64
|
|
|
|
65
|
|
|
try { |
66
|
3 |
|
$em->flush(); |
67
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
68
|
|
|
// it's ok |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
return new JsonResponse(['id' => $interestedMovie->getId()], 202); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @Route("/api/users/interestedMovies/{id<\d+>}", methods={"DELETE"}); |
76
|
|
|
* |
77
|
|
|
* @param int $id |
78
|
|
|
* @param InterestedMovieRepository $repository |
79
|
|
|
* @param EntityManagerInterface $em |
80
|
|
|
* |
81
|
|
|
* @return JsonResponse |
82
|
|
|
*/ |
83
|
2 |
|
public function deleteInterestedMovies(int $id, InterestedMovieRepository $repository, EntityManagerInterface $em) |
84
|
|
|
{ |
85
|
2 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
86
|
|
|
|
87
|
|
|
/** @var $currentUser User */ |
88
|
2 |
|
$currentUser = $this->getUser(); |
89
|
|
|
|
90
|
2 |
|
if (null === $interestedMovie = $repository->findOneById($id, $currentUser->getId())) { |
91
|
2 |
|
$interestedMovie = $repository->findOneByMovieId($id, $currentUser->getId()); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
if (null === $interestedMovie) { |
95
|
|
|
return new JsonResponse(null, 202); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$em->remove($interestedMovie); |
99
|
2 |
|
$em->flush(); |
100
|
|
|
|
101
|
2 |
|
return new JsonResponse(null, 202); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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: