Completed
Push — master ( 49afcb...98eb90 )
by Valentyn
05:04
created

InterestedMovieController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 10
dl 0
loc 83
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 16 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()),
37 3
            $offset,
38 3
            $limit ? (int) $limit : null,
39 3
            false
40
        );
41
42 3
        return $this->response($interestedMovies, 200, [], [
43 3
            'groups' => ['list'],
44
        ]);
45
    }
46
47
    /**
48
     * @Route("/api/users/interestedMovies", methods={"POST"});
49
     *
50
     * @param AddInterestedMovieRequest $request
51
     * @param EntityManagerInterface $em
52
     * @return JsonResponse
53
     * @throws \Exception
54
     */
55 3
    public function postInterestedMovies(AddInterestedMovieRequest $request, EntityManagerInterface $em)
56
    {
57 3
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
58
59
        /** @var $movie Movie */
60 3
        $movie = $em->getReference(Movie::class, $request->get('movie_id'));
61 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...
62 3
        $em->persist($interestedMovie);
63
64
        try {
65 3
            $em->flush();
66
        } catch (UniqueConstraintViolationException $exception) {
67
            // it's ok
68
        }
69
70 3
        return new JsonResponse(['id' => $interestedMovie->getId()], 202);
71
    }
72
73
    /**
74
     * @Route("/api/users/interestedMovies/{id<\d+>}", methods={"DELETE"});
75
     *
76
     * @param int $id
77
     * @param InterestedMovieRepository $repository
78
     * @param EntityManagerInterface $em
79
     * @return JsonResponse
80
     */
81 2
    public function deleteInterestedMovies(int $id, InterestedMovieRepository $repository, EntityManagerInterface $em)
82
    {
83 2
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
84
85
        /** @var $currentUser User */
86 2
        $currentUser = $this->getUser();
87
88 2
        if (null === $interestedMovie = $repository->findOneById($id, $currentUser->getId())) {
89 1
            $interestedMovie = $repository->findOneByMovieId($id, $currentUser->getId());
90
        }
91
92 2
        if (null === $interestedMovie) {
93
            return new JsonResponse(null, 202);
94
        }
95
96 2
        $em->remove($interestedMovie);
97 2
        $em->flush();
98
99 2
        return new JsonResponse(null, 202);
100
    }
101
}
102