Completed
Push — master ( d44c39...321b0a )
by Valentyn
04:27
created

WatchedMovieController::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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\Movies\Transformer\MovieTransformer;
12
use App\Pagination\CustomPaginatedCollection;
13
use App\Pagination\PaginatedCollection;
14
use App\Users\Entity\User;
15
use App\Users\Entity\UserWatchedMovie;
16
use App\Users\Repository\WatchedMovieRepository;
17
use App\Users\Request\MergeWatchedMoviesRequest;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
use Symfony\Component\Routing\Annotation\Route;
24
25
class WatchedMovieController extends BaseController
26
{
27
    /**
28
     * @Route("/api/users/watchedMovies", methods={"POST"});
29
     *
30
     * @param AddWatchedMovieRequest $addWatchedMovieRequest
31
     * @param Request                $request
32
     * @param WatchedMovieService    $watchedMovieService
33
     *
34
     * @throws \Exception|NotFoundHttpException
35
     *
36
     * @return JsonResponse
37
     */
38 6
    public function postWatchedMovies(AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService)
39
    {
40 6
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
41
42 6
        $watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO();
43 6
        $isMovieAdded = $watchedMovieService->addUserWatchedMovie($this->getUser(), $watchedMovieDTO, $request->getLocale());
0 ignored issues
show
Documentation introduced by
$this->getUser() is of type null|object, but the function expects a object<App\Users\Entity\User>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45 6
        if ($isMovieAdded === false) {
46
            throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID');
47
        }
48
49 6
        return new JsonResponse(null, 202);
50
    }
51
52
    /**
53
     * @Route("/api/users/{user}/watchedMovies/{watchedMovie}", methods={"PATCH"});
54
     *
55
     * @param UserWatchedMovie          $watchedMovie
56
     * @param UpdateWatchedMovieRequest $request
57
     * @param WatchedMovieService       $watchedMovieService
58
     *
59
     * @throws \Exception
60
     *
61
     * @return JsonResponse
62
     */
63 1
    public function patchWatchedMoviesById(UserWatchedMovie $watchedMovie, UpdateWatchedMovieRequest $request, WatchedMovieService $watchedMovieService)
64
    {
65 1
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
66
67 1
        if ($watchedMovie->getUser()->getId() !== $this->getUser()->getId()) {
68
            throw new AccessDeniedHttpException();
69
        }
70
71 1
        $watchedMovieDTO = $request->getWatchedMovieDTO();
72 1
        $watchedMovieService->updateUserWatchedMovie($watchedMovie, $watchedMovieDTO);
73
74 1
        $this->getDoctrine()->getManager()->flush();
75
76 1
        return new JsonResponse(null, 200);
77
    }
78
79
    /**
80
     * @Route("/api/users/{user}/watchedMovies/movie/{movieId}", methods={"PATCH"});
81
     *
82
     * @param int                       $movieId
83
     * @param UpdateWatchedMovieRequest $request
84
     * @param WatchedMovieService       $watchedMovieService
85
     *
86
     * @throws \Exception
87
     *
88
     * @return JsonResponse
89
     */
90 1
    public function patchWatchedMoviesByMovieId(int $movieId, UpdateWatchedMovieRequest $request, WatchedMovieService $watchedMovieService, WatchedMovieRepository $repository)
91
    {
92 1
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
93
94
        /** @var $user User */
95 1
        $user = $this->getUser();
96
97 1
        if (null === $watchedMovie = $repository->findOneByMovieId($movieId, $user->getId())) {
98
            throw new NotFoundHttpException();
99
        }
100
101 1
        $watchedMovieDTO = $request->getWatchedMovieDTO();
102 1
        $watchedMovieService->updateUserWatchedMovie($watchedMovie, $watchedMovieDTO);
103
104 1
        $this->getDoctrine()->getManager()->flush();
105
106 1
        return new JsonResponse(null, 200);
107
    }
108
109
    /**
110
     * @Route("/api/users/{id<\d+>}/watchedMovies", methods={"GET"});
111
     *
112
     * @param Request         $request
113
     * @param User            $profileOwner
114
     * @param MovieRepository $repository
115
     *
116
     * @return JsonResponse
117
     */
118 2
    public function getAll(Request $request, User $profileOwner, MovieRepository $repository)
119
    {
120 2
        $offset = (int) $request->get('offset', 0);
121 2
        $limit = $request->get('limit', null);
122
123 2
        $currentUser = $this->getUser();
124
125 2
        [$items, $ids, $count] = $repository->getAllWatchedMoviesByUserId($profileOwner, $currentUser);
0 ignored issues
show
Bug introduced by
The variable $items does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $ids does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $count does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
126 2
        $collection = new CustomPaginatedCollection($items, $ids, $count, $offset, $limit);
127
128 2
        return $this->items($collection, MovieTransformer::list());
129
    }
130
131
    /**
132
     * @Route("/api/users/{user}/watchedMovies/{watchedMovieId}", methods={"DELETE"});
133
     *
134
     * @param int                    $watchedMovieId
135
     * @param WatchedMovieRepository $repository
136
     *
137
     * @return JsonResponse
138
     */
139
    public function deleteWatchedMovies(int $watchedMovieId, WatchedMovieRepository $repository)
140
    {
141
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
142
143
        /** @var $currentUser User */
144
        $currentUser = $this->getUser();
145
146
        if (null === $watchedMovie = $repository->findOneById($watchedMovieId, $currentUser->getId())) {
147
            $watchedMovie = $repository->findOneByMovieId($watchedMovieId, $currentUser->getId());
148
        }
149
150
        if (null === $watchedMovie) {
151
            throw new NotFoundHttpException();
152
        }
153
154
        $this->getDoctrine()->getManager()->remove($watchedMovie);
155
        $this->getDoctrine()->getManager()->flush();
156
157
        return new JsonResponse(null, 202);
158
    }
159
160
    /**
161
     * @Route("/api/users/mergeWatchedMovies", methods={"POST"});
162
     *
163
     * @param MergeWatchedMoviesRequest $mergeWatchedMoviesRequest
164
     * @param WatchedMovieService       $watchedMovieService
165
     *
166
     * @throws \Exception
167
     *
168
     * @return JsonResponse
169
     */
170
    public function postMergeWatchedMovies(MergeWatchedMoviesRequest $mergeWatchedMoviesRequest, WatchedMovieService $watchedMovieService)
171
    {
172
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
173
        $guestSessionRepository = $this->getDoctrine()->getRepository(GuestSession::class);
174
        $guestSession = $guestSessionRepository->findOneBy([
175
            'token' => $mergeWatchedMoviesRequest->get('token'),
176
        ]);
177
178
        /** @var $guestSession GuestSession|null */
179
        if ($guestSession === null) {
180
            throw new NotFoundHttpException('Guest session not found by provided token');
181
        }
182
183
        $watchedMovieService->mergeWatchedMovies($guestSession, $this->getUser());
0 ignored issues
show
Documentation introduced by
$this->getUser() is of type null|object, but the function expects a object<App\Users\Entity\User>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
184
185
        return new JsonResponse(null, 202);
186
    }
187
}
188