Completed
Push — master ( 338899...97cb5e )
by Valentyn
03:07
created

MovieController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 10
dl 0
loc 86
ccs 20
cts 26
cp 0.7692
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 13 2
A getMovies() 0 6 1
A getSearch() 0 9 1
A postMovies() 0 19 2
1
<?php
2
3
namespace App\Movies\Controller;
4
5
use App\Controller\BaseController;
6
use App\Movies\Entity\Movie;
7
use App\Movies\Request\CreateMovieRequest;
8
use App\Movies\Request\SearchRequest;
9
use App\Movies\Service\MovieManageService;
10
use App\Movies\Service\SearchService;
11
use App\Users\Entity\UserRoles;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
/**
17
 * Class MovieController
18
 * @package App\Movies\Controller
19
 */
20
class MovieController extends BaseController
21
{
22
    /**
23
     * Get all movies
24
     *
25
     * @Route("/api/movies", methods={"GET"})
26
     */
27 3
    public function getAll()
28
    {
29 3
        if ($this->getUser()) {
30
            $userId = $this->getUser()->getId();
31
            $movies = $this->getDoctrine()->getRepository(Movie::class)->findAllWithIsWatchedFlag($userId);
0 ignored issues
show
Bug introduced by
The method findAllWithIsWatchedFlag() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findAll()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
        } else {
33 3
            $movies = $this->getDoctrine()->getRepository(Movie::class)->findAll();
34
        }
35
36 3
        return $this->response($movies, 200, [], [
37 3
            'groups' => ['list'],
38
        ]);
39
    }
40
41
    /**
42
     * Get movie resource
43
     *
44
     * @Route("/api/movies/{id}", methods={"GET"})
45
     * @param Movie $movie
46
     * @return \Symfony\Component\HttpFoundation\JsonResponse
47
     */
48
    public function getMovies(Movie $movie)
49
    {
50
        return $this->response($movie, 200, [], [
51
            'groups' => ['view'],
52
        ]);
53
    }
54
55
    /**
56
     * Get movies by title
57
     *
58
     * @Route("/api/movies/search", methods={"POST"})
59
     * @param SearchRequest $request
60
     * @param SearchService $searchService
61
     * @param Request $currentRequest
62
     * @throws \Exception
63
     * @return \Symfony\Component\HttpFoundation\JsonResponse
64
     */
65 2
    public function getSearch(SearchRequest $request, SearchService $searchService, Request $currentRequest)
66
    {
67 2
        $query = $request->get('query');
68 2
        $movies = $searchService->findByQuery($query, $currentRequest->getLocale());
69
70 2
        return $this->response($movies, 200, [], [
71 2
            'groups' => ['list']
72
        ]);
73
    }
74
75
    /**
76
     * Create new movie
77
     *
78
     * @Route("/api/movies", methods={"POST"})
79
     *
80
     * @param CreateMovieRequest $request
81
     * @param MovieManageService $service
82
     * @param ValidatorInterface $validator
83
     * @throws \Exception
84
     * @return \Symfony\Component\HttpFoundation\JsonResponse
85
     */
86 2
    public function postMovies(CreateMovieRequest $request, MovieManageService $service, ValidatorInterface $validator)
87
    {
88 2
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
89
90 1
        $movie = $service->createMovieByRequest($request);
91 1
        $errors = $validator->validate($movie);
92
93 1
        if (count($errors)) {
94
            return $request->getErrorResponse($errors);
95
        }
96
97 1
        $entityManager = $this->getDoctrine()->getManager();
98 1
        $entityManager->persist($movie);
99 1
        $entityManager->flush();
100
101 1
        return $this->response($movie, 200, [], [
102 1
            'groups' => ['view'],
103
        ]);
104
    }
105
}