Completed
Push — master ( ff1980...6d4fa7 )
by Valentyn
03:53
created

InterestedMovieController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 10
dl 0
loc 82
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 15 2
A postInterestedMovies() 0 17 2
A deleteInterestedMovies() 0 20 3
1
<?php
2
3
namespace App\Users\Controller;
4
5
use App\Controller\BaseController;
6
use App\Movies\Entity\Movie;
7
use App\Movies\Repository\MovieRepository;
8
use App\Pagination\PaginatedCollection;
9
use App\Users\Entity\User;
10
use App\Users\Entity\UserInterestedMovie;
11
use App\Users\Repository\InterestedMovieRepository;
12
use App\Users\Request\AddInterestedMovieRequest;
13
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Routing\Annotation\Route;
18
19
class InterestedMovieController extends BaseController
20
{
21
    /**
22
     * @Route("/api/users/{id<\d+>}/interestedMovies", methods={"GET"});
23
     *
24
     * @param Request         $request
25
     * @param User            $user
26
     * @param MovieRepository $repository
27
     *
28
     * @return JsonResponse
29
     */
30 3
    public function getAll(Request $request, User $user, MovieRepository $repository)
31
    {
32 3
        $offset = (int) $request->get('offset', 0);
33 3
        $limit = $request->get('limit', null);
34
35 3
        $interestedMovies = new PaginatedCollection(
36 3
            $repository->getAllInterestedMoviesByUserId($user->getId(), $this->getUser()),
37 3
            $offset,
38 3
            $limit ? (int) $limit : null
39
        );
40
41 3
        return $this->response($interestedMovies, 200, [], [
42 3
            'groups' => ['list'],
43
        ]);
44
    }
45
46
    /**
47
     * @Route("/api/users/interestedMovies", methods={"POST"});
48
     *
49
     * @param AddInterestedMovieRequest $request
50
     * @param EntityManagerInterface $em
51
     * @return JsonResponse
52
     * @throws \Exception
53
     */
54 3
    public function postInterestedMovies(AddInterestedMovieRequest $request, EntityManagerInterface $em)
55
    {
56 3
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
57
58
        /** @var $movie Movie */
59 3
        $movie = $em->getReference(Movie::class, $request->get('movie_id'));
60 3
        $interestedMovie = new UserInterestedMovie($this->getUser(), $movie);
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...
61 3
        $em->persist($interestedMovie);
62
63
        try {
64 3
            $em->flush();
65
        } catch (UniqueConstraintViolationException $exception) {
66
            // it's ok
67
        }
68
69 3
        return new JsonResponse(['id' => $interestedMovie->getId()], 202);
70
    }
71
72
    /**
73
     * @Route("/api/users/interestedMovies/{id<\d+>}", methods={"DELETE"});
74
     *
75
     * @param int $id
76
     * @param InterestedMovieRepository $repository
77
     * @param EntityManagerInterface $em
78
     * @return JsonResponse
79
     */
80 2
    public function deleteInterestedMovies(int $id, InterestedMovieRepository $repository, EntityManagerInterface $em)
81
    {
82 2
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
83
84
        /** @var $currentUser User */
85 2
        $currentUser = $this->getUser();
86
87 2
        if (null === $interestedMovie = $repository->findOneById($id, $currentUser->getId())) {
88 2
            $interestedMovie = $repository->findOneByMovieId($id, $currentUser->getId());
89
        }
90
91 2
        if (null === $interestedMovie) {
92
            return new JsonResponse(null, 202);
93
        }
94
95 2
        $em->remove($interestedMovie);
96 2
        $em->flush();
97
98 2
        return new JsonResponse(null, 202);
99
    }
100
}
101