Completed
Push — master ( 9d729c...78ea8b )
by Valentyn
03:06
created

WatchedMovieController::postWatchedMovies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0116
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\Service\WatchedMovieService;
10
use App\Pagination\PaginatedCollection;
11
use App\Users\Entity\User;
12
use App\Users\Entity\UserWatchedMovie;
13
use App\Users\Repository\WatchedMovieRepository;
14
use App\Users\Request\MergeWatchedMoviesRequest;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
21
22
class WatchedMovieController extends BaseController
23
{
24
    /**
25
     * @Route("/api/users/watchedMovies", methods={"POST"});
26
     *
27
     * @param AddWatchedMovieRequest $addWatchedMovieRequest
28
     * @param Request                $request
29
     * @param WatchedMovieService    $watchedMovieService
30
     *
31
     * @throws \Exception|NotFoundHttpException
32
     *
33
     * @return JsonResponse
34
     */
35 2
    public function postWatchedMovies(AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService)
36
    {
37 2
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
38
39 2
        $watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO();
40 2
        $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...
41
42 2
        if ($isMovieAdded === false) {
43
            throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID');
44
        }
45
46 2
        return new JsonResponse(null, 202);
47
    }
48
49
    /**
50
     * @Route("/api/users/{id<\d+>}/watchedMovies", methods={"GET"});
51
     *
52
     * @param Request         $request
53
     * @param User            $user
54
     * @param MovieRepository $repository
55
     *
56
     * @return JsonResponse
57
     */
58
    public function getAll(Request $request, User $user, MovieRepository $repository)
59
    {
60
        $offset = (int) $request->get('offset', 0);
61
        $limit = $request->get('limit', null);
62
63
        $watchedMovies = new PaginatedCollection(
64
            $repository->getAllWatchedMoviesByUserId($user->getId()),
65
            $offset,
66
            $limit ? (int) $limit : null
67
        );
68
69
        return $this->response($watchedMovies, 200, [], [
70
            'groups' => ['list'],
71
        ]);
72
    }
73
74
    /**
75
     * @Route("/api/users/{username}/watchedMovies", methods={"GET"});
76
     * @ParamConverter("user", options={"mapping"={"username"="username"}})
77
     *
78
     * @param Request         $request
79
     * @param User            $user
80
     * @param MovieRepository $repository
81
     *
82
     * @return JsonResponse
83
     */
84
    public function getAllByUsername(Request $request, User $user, MovieRepository $repository)
85
    {
86
        $offset = (int) $request->get('offset', 0);
87
        $limit = $request->get('limit', null);
88
89
        $watchedMovies = new PaginatedCollection(
90
            $repository->getAllWatchedMoviesByUserId($user->getId()),
91
            $offset,
92
            $limit ? (int) $limit : null
93
        );
94
95
        return $this->response($watchedMovies, 200, [], [
96
            'groups' => ['list'],
97
        ]);
98
    }
99
100
    /**
101
     * @Route("/api/users/{user}/watchedMovies/{watchedMovieId}", methods={"DELETE"});
102
     * @param int $watchedMovieId
103
     * @param WatchedMovieRepository $repository
104
     * @return JsonResponse
105
     */
106
    public function deleteWatchedMovies(int $watchedMovieId, WatchedMovieRepository $repository)
107
    {
108
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
109
110
        /** @var $currentUser User */
111
        $currentUser = $this->getUser();
112
113
        if (null === $watchedMovie = $repository->find($watchedMovieId)) {
114
            $watchedMovie = $repository->findOneByMovieId($watchedMovieId, $currentUser->getId());
115
        }
116
117
        if (null === $watchedMovie) {
118
            throw new NotFoundHttpException();
119
        }
120
121
        if ($watchedMovie->getUser()->getId() !== $currentUser->getId()) {
122
            throw new AccessDeniedHttpException();
123
        }
124
125
        $this->getDoctrine()->getManager()->remove($watchedMovie);
126
        $this->getDoctrine()->getManager()->flush();
127
128
        return new JsonResponse(null, 202);
129
    }
130
131
    /**
132
     * @Route("/api/users/mergeWatchedMovies", methods={"POST"});
133
     *
134
     * @param MergeWatchedMoviesRequest $mergeWatchedMoviesRequest
135
     * @param WatchedMovieService       $watchedMovieService
136
     *
137
     * @throws \Exception
138
     *
139
     * @return JsonResponse
140
     */
141
    public function postMergeWatchedMovies(MergeWatchedMoviesRequest $mergeWatchedMoviesRequest, WatchedMovieService $watchedMovieService)
142
    {
143
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
144
        $guestSessionRepository = $this->getDoctrine()->getRepository(GuestSession::class);
145
        $guestSession = $guestSessionRepository->findOneBy([
146
            'token' => $mergeWatchedMoviesRequest->get('token'),
147
        ]);
148
149
        /** @var $guestSession GuestSession|null */
150
        if ($guestSession === null) {
151
            throw new NotFoundHttpException('Guest session not found by provided token');
152
        }
153
154
        $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...
155
156
        return new JsonResponse(null, 202);
157
    }
158
}
159