Completed
Push — master ( 1302da...3a2ccf )
by Valentyn
03:07
created

MovieController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 80.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 10
dl 0
loc 94
ccs 25
cts 31
cp 0.8065
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 18 2
A getMovies() 0 6 1
A getSearch() 0 12 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\Pagination\PaginatedCollection;
12
use App\Users\Entity\UserRoles;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\Validator\Validator\ValidatorInterface;
16
17
/**
18
 * Class MovieController
19
 * @package App\Movies\Controller
20
 */
21
class MovieController extends BaseController
22
{
23
    /**
24
     * Get all movies
25
     *
26
     * @Route("/api/movies", methods={"GET"})
27
     */
28 3
    public function getAll(Request $request)
29
    {
30 3
        if ($this->getUser()) {
31
            $userId = $this->getUser()->getId();
32
            $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...
33
        } else {
34 3
            $movies = $this->getDoctrine()->getRepository(Movie::class)->findAllQuery();
0 ignored issues
show
Bug introduced by
The method findAllQuery() 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...
35
        }
36
37 3
        $offset = (int)$request->get('offset', 0);
38 3
        $limit = $request->get('limit', null);
39
40 3
        $movies = new PaginatedCollection($movies, $offset, $limit);
41
42 3
        return $this->response($movies, 200, [], [
43 3
            'groups' => ['list'],
44
        ]);
45
    }
46
47
    /**
48
     * Get movie resource
49
     *
50
     * @Route("/api/movies/{id}", methods={"GET"})
51
     * @param Movie $movie
52
     * @return \Symfony\Component\HttpFoundation\JsonResponse
53
     */
54
    public function getMovies(Movie $movie)
55
    {
56
        return $this->response($movie, 200, [], [
57
            'groups' => ['view'],
58
        ]);
59
    }
60
61
    /**
62
     * Get movies by title
63
     *
64
     * @Route("/api/movies/search", methods={"POST"})
65
     * @param SearchRequest $request
66
     * @param SearchService $searchService
67
     * @param Request $currentRequest
68
     * @throws \Exception
69
     * @return \Symfony\Component\HttpFoundation\JsonResponse
70
     */
71 2
    public function getSearch(SearchRequest $request, SearchService $searchService, Request $currentRequest)
72
    {
73 2
        $offset = (int)$request->get('offset', 0);
74 2
        $limit = $request->get('limit', null);
75
76 2
        $query = $request->get('query');
77 2
        $movies = $searchService->findByQuery($query, $currentRequest->getLocale(), $offset, $limit);
78
79 2
        return $this->response($movies, 200, [], [
80 2
            'groups' => ['list']
81
        ]);
82
    }
83
84
    /**
85
     * Create new movie
86
     *
87
     * @Route("/api/movies", methods={"POST"})
88
     *
89
     * @param CreateMovieRequest $request
90
     * @param MovieManageService $service
91
     * @param ValidatorInterface $validator
92
     * @throws \Exception
93
     * @return \Symfony\Component\HttpFoundation\JsonResponse
94
     */
95 2
    public function postMovies(CreateMovieRequest $request, MovieManageService $service, ValidatorInterface $validator)
96
    {
97 2
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
98
99 1
        $movie = $service->createMovieByRequest($request);
100 1
        $errors = $validator->validate($movie);
101
102 1
        if (count($errors)) {
103
            return $request->getErrorResponse($errors);
104
        }
105
106 1
        $entityManager = $this->getDoctrine()->getManager();
107 1
        $entityManager->persist($movie);
108 1
        $entityManager->flush();
109
110 1
        return $this->response($movie, 200, [], [
111 1
            'groups' => ['view'],
112
        ]);
113
    }
114
}