Completed
Push — master ( 61c86f...9d729c )
by Valentyn
04:45
created

WatchedMovieController   B

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 16.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 16
dl 0
loc 111
ccs 6
cts 37
cp 0.1622
rs 8.4614

4 Methods

Rating   Name   Duplication   Size   Complexity  
A postWatchedMovies() 0 13 2
A getAll() 0 15 2
B deleteWatchedMovies() 0 24 4
A postMergeWatchedMovies() 0 17 2
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
21
class WatchedMovieController extends BaseController
22
{
23
    /**
24
     * @Route("/api/users/watchedMovies", methods={"POST"});
25
     *
26
     * @param AddWatchedMovieRequest $addWatchedMovieRequest
27
     * @param Request                $request
28
     * @param WatchedMovieService    $watchedMovieService
29
     *
30
     * @throws \Exception|NotFoundHttpException
31
     *
32
     * @return JsonResponse
33
     */
34 2
    public function postWatchedMovies(AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService)
35
    {
36 2
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
37
38 2
        $watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO();
39 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...
40
41 2
        if ($isMovieAdded === false) {
42
            throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID');
43
        }
44
45 2
        return new JsonResponse(null, 202);
46
    }
47
48
    /**
49
     * @Route("/api/users/{id}/watchedMovies", methods={"GET"});
50
     *
51
     * @param Request         $request
52
     * @param User            $user
53
     * @param MovieRepository $repository
54
     *
55
     * @return JsonResponse
56
     */
57
    public function getAll(Request $request, User $user, MovieRepository $repository)
58
    {
59
        $offset = (int) $request->get('offset', 0);
60
        $limit = $request->get('limit', null);
61
62
        $watchedMovies = new PaginatedCollection(
63
            $repository->getAllWatchedMoviesByUserId($user->getId()),
64
            $offset,
65
            $limit ? (int) $limit : null
66
        );
67
68
        return $this->response($watchedMovies, 200, [], [
69
            'groups' => ['list'],
70
        ]);
71
    }
72
73
    /**
74
     * @Route("/api/users/{user}/watchedMovies/{watchedMovieId}", methods={"DELETE"});
75
     * @param int $watchedMovieId
76
     * @param WatchedMovieRepository $repository
77
     * @return JsonResponse
78
     */
79
    public function deleteWatchedMovies(int $watchedMovieId, WatchedMovieRepository $repository)
80
    {
81
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
82
83
        /** @var $currentUser User */
84
        $currentUser = $this->getUser();
85
86
        if (null === $watchedMovie = $repository->find($watchedMovieId)) {
87
            $watchedMovie = $repository->findOneByMovieId($watchedMovieId, $currentUser->getId());
88
        }
89
90
        if (null === $watchedMovie) {
91
            throw new NotFoundHttpException();
92
        }
93
94
        if ($watchedMovie->getUser()->getId() !== $currentUser->getId()) {
95
            throw new AccessDeniedHttpException();
96
        }
97
98
        $this->getDoctrine()->getManager()->remove($watchedMovie);
99
        $this->getDoctrine()->getManager()->flush();
100
101
        return new JsonResponse(null, 202);
102
    }
103
104
    /**
105
     * @Route("/api/users/mergeWatchedMovies", methods={"POST"});
106
     *
107
     * @param MergeWatchedMoviesRequest $mergeWatchedMoviesRequest
108
     * @param WatchedMovieService       $watchedMovieService
109
     *
110
     * @throws \Exception
111
     *
112
     * @return JsonResponse
113
     */
114
    public function postMergeWatchedMovies(MergeWatchedMoviesRequest $mergeWatchedMoviesRequest, WatchedMovieService $watchedMovieService)
115
    {
116
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
117
        $guestSessionRepository = $this->getDoctrine()->getRepository(GuestSession::class);
118
        $guestSession = $guestSessionRepository->findOneBy([
119
            'token' => $mergeWatchedMoviesRequest->get('token'),
120
        ]);
121
122
        /** @var $guestSession GuestSession|null */
123
        if ($guestSession === null) {
124
            throw new NotFoundHttpException('Guest session not found by provided token');
125
        }
126
127
        $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...
128
129
        return new JsonResponse(null, 202);
130
    }
131
}
132