Completed
Push — master ( 1a9043...984777 )
by Valentyn
08:31
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\DTO\WatchedMovieDTO;
8
use App\Movies\Entity\Movie;
9
use App\Pagination\PaginatedCollection;
10
use App\Pagination\PaginatorBuilder;
11
use App\Users\Entity\User;
12
use App\Users\Entity\UserWatchedMovie;
13
use App\Movies\Repository\MovieRepository;
14
use App\Movies\Service\SearchService;
15
use App\Movies\Request\AddWatchedMovieRequest;
16
use App\Movies\Service\WatchedMovieService;
17
use App\Users\Repository\WatchedMovieRepository;
18
use App\Users\Request\MergeWatchedMoviesRequest;
19
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
20
use Doctrine\ORM\Tools\Pagination\Paginator;
21
use Symfony\Component\HttpFoundation\JsonResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
use Symfony\Component\Routing\Annotation\Route;
26
27
class WatchedMovieController extends BaseController
28
{
29
    /**
30
     * @Route("/api/users/watchedMovies", methods={"POST"});
31
     * @param AddWatchedMovieRequest $addWatchedMovieRequest
32
     * @param Request $request
33
     * @param WatchedMovieService $watchedMovieService
34
     * @return JsonResponse
35
     * @throws \Exception|NotFoundHttpException
36
     */
37 2
    public function postWatchedMovies(AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService)
38
    {
39 2
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
40
41 2
        $watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO();
42 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...
43
44 2
        if ($isMovieAdded === false) {
45
            throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID');
46
        }
47
48 2
        return new JsonResponse(null, 202);
49
    }
50
51
    /**
52
     * @Route("/api/users/{id}/watchedMovies", methods={"GET"});
53
     * @param Request $request
54
     * @param User $user
55
     * @return JsonResponse
56
     */
57
    public function getAll(Request $request, User $user)
58
    {
59
        $em = $this->getDoctrine()->getManager();
60
        /** @var $watchedMovieRepository WatchedMovieRepository */
61
        $watchedMovieRepository = $em->getRepository(UserWatchedMovie::class);
62
63
        $offset = (int)$request->get('offset', 0);
64
        $limit = $request->get('limit', null);
65
66
        $watchedMovies = new PaginatedCollection(
67
            $watchedMovieRepository->getAllWatchedMoviesByUserId($user->getId()),
68
            $offset,
69
            $limit ? (int)$limit : null
70
        );
71
72
        return $this->response($watchedMovies, 200, [], [
73
            'groups' => ['list']
74
        ]);
75
    }
76
77
    /**
78
     * @Route("/api/users/mergeWatchedMovies", methods={"POST"});
79
     * @param MergeWatchedMoviesRequest $mergeWatchedMoviesRequest
80
     * @param WatchedMovieService $watchedMovieService
81
     * @throws \Exception
82
     * @return JsonResponse
83
     */
84
    public function postMergeWatchedMovies(MergeWatchedMoviesRequest $mergeWatchedMoviesRequest, WatchedMovieService $watchedMovieService)
85
    {
86
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
87
        $guestSessionRepository = $this->getDoctrine()->getRepository(GuestSession::class);
88
        $guestSession = $guestSessionRepository->findOneBy([
89
            'token' => $mergeWatchedMoviesRequest->get('token')
90
        ]);
91
92
        /** @var $guestSession GuestSession|null */
93
        if ($guestSession === null) {
94
            throw new NotFoundHttpException('Guest session not found by provided token');
95
        }
96
97
        $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...
98
99
        return new JsonResponse(null, 202);
100
    }
101
}