Completed
Push — master ( 71eba9...1e1a94 )
by Valentyn
04:31
created

MovieCardController::deleteMoviesCards()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
cc 2
nc 2
nop 2
crap 2.0116
1
<?php
2
3
namespace App\Movies\Controller;
4
5
use App\Controller\BaseController;
6
use App\Movies\Entity\Movie;
7
use App\Movies\Entity\MovieCard;
8
use App\Movies\Repository\MovieCardRepository;
9
use App\Movies\Request\NewMovieCardRequest;
10
use App\Pagination\PaginatedCollection;
11
use App\Users\Entity\UserRoles;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
class MovieCardController extends BaseController
18
{
19
    /**
20
     * @Route("/api/movies/{id}/cards", methods={"GET"}, requirements={"id"="\d+"})
21
     *
22
     * @param Request              $request
23
     * @param int                  $id
24
     * @param MovieCardRepository  $repository
25
     *
26
     * @return JsonResponse
27
     */
28 1
    public function getMoviesCards(Request $request, int $id, MovieCardRepository $repository)
29
    {
30 1
        $cards = $repository->findAllByMovie($request->getLocale(), $id);
31
32 1
        $offset = (int) $request->get('offset', 0);
33 1
        $limit = $request->get('limit', null);
34
35 1
        $collection = new PaginatedCollection($cards, $offset, $limit);
36
37 1
        return $this->response($collection, 200, [], [
38 1
            'groups' => ['list'],
39
        ]);
40
    }
41
42
    /**
43
     * Add new recommendation.
44
     *
45
     * @Route("/api/movies/{id}/cards", methods={"POST"})
46
     *
47
     * @throws \Doctrine\ORM\ORMException
48
     *
49
     * @return JsonResponse
50
     */
51 1
    public function postMoviesCards(Request $request, NewMovieCardRequest $cardRequest, Movie $movie, EntityManagerInterface $em)
52
    {
53 1
        $this->denyAccessUnlessGranted(UserRoles::ROLE_USER);
54
55 1
        $card = $cardRequest->get('card');
56 1
        $user = $this->getUser();
57
58 1
        $card = new MovieCard(
59 1
            $movie,
60 1
            $user,
0 ignored issues
show
Documentation introduced by
$user 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 1
            $request->getLocale(),
62 1
            $card['title'],
63 1
            $card['description'],
64 1
            $card['type'],
65 1
            $card['url']
66
        );
67
68 1
        $em->persist($card);
69 1
        $em->flush();
70
71 1
        return new JsonResponse();
72
    }
73
74
    /**
75
     * @Route("/api/movies/{movie_id}/cards/{id}", methods={"DELETE"})
76
     */
77 1
    public function deleteMoviesCards(MovieCard $card, EntityManagerInterface $em)
78
    {
79 1
        $this->denyAccessUnlessGranted(UserRoles::ROLE_USER);
80
81 1
        if ($card->getUser()->getId() !== $this->getUser()->getId()) {
82
            $this->denyAccessUnlessGranted([UserRoles::ROLE_MODERATOR, UserRoles::ROLE_ADMIN]);
83
        }
84
85 1
        $em->remove($card);
86 1
        $em->flush();
87
88 1
        return new JsonResponse();
89
    }
90
}
91