Completed
Push — master ( dd713c...61c86f )
by Valentyn
05:15
created

WatchedMovieController::postMergeWatchedMovies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 6
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\NotFoundHttpException;
18
use Symfony\Component\Routing\Annotation\Route;
19
20
class WatchedMovieController extends BaseController
21
{
22
    /**
23
     * @Route("/api/users/watchedMovies", methods={"POST"});
24
     *
25
     * @param AddWatchedMovieRequest $addWatchedMovieRequest
26
     * @param Request                $request
27
     * @param WatchedMovieService    $watchedMovieService
28
     *
29
     * @throws \Exception|NotFoundHttpException
30
     *
31
     * @return JsonResponse
32
     */
33 2
    public function postWatchedMovies(AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService)
34
    {
35 2
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
36
37 2
        $watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO();
38 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...
39
40 2
        if ($isMovieAdded === false) {
41
            throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID');
42
        }
43
44 2
        return new JsonResponse(null, 202);
45
    }
46
47
    /**
48
     * @Route("/api/users/{id}/watchedMovies", methods={"GET"});
49
     *
50
     * @param Request $request
51
     * @param User    $user
52
     *
53
     * @return JsonResponse
54
     */
55
    public function getAll(Request $request, User $user, MovieRepository $repository)
56
    {
57
        $offset = (int) $request->get('offset', 0);
58
        $limit = $request->get('limit', null);
59
60
        $watchedMovies = new PaginatedCollection(
61
            $repository->getAllWatchedMoviesByUserId($user->getId()),
62
            $offset,
63
            $limit ? (int) $limit : null
64
        );
65
66
        return $this->response($watchedMovies, 200, [], [
67
            'groups' => ['list'],
68
        ]);
69
    }
70
71
    /**
72
     * @Route("/api/users/mergeWatchedMovies", methods={"POST"});
73
     *
74
     * @param MergeWatchedMoviesRequest $mergeWatchedMoviesRequest
75
     * @param WatchedMovieService       $watchedMovieService
76
     *
77
     * @throws \Exception
78
     *
79
     * @return JsonResponse
80
     */
81
    public function postMergeWatchedMovies(MergeWatchedMoviesRequest $mergeWatchedMoviesRequest, WatchedMovieService $watchedMovieService)
82
    {
83
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
84
        $guestSessionRepository = $this->getDoctrine()->getRepository(GuestSession::class);
85
        $guestSession = $guestSessionRepository->findOneBy([
86
            'token' => $mergeWatchedMoviesRequest->get('token'),
87
        ]);
88
89
        /** @var $guestSession GuestSession|null */
90
        if ($guestSession === null) {
91
            throw new NotFoundHttpException('Guest session not found by provided token');
92
        }
93
94
        $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...
95
96
        return new JsonResponse(null, 202);
97
    }
98
}
99