Completed
Push — master ( 97cb5e...1302da )
by Valentyn
02:35
created

MovieController::getSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
crap 1
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
        if (count($errors)) {
94
            return $request->getErrorResponse($errors);
95
        }
96
97
        $entityManager = $this->getDoctrine()->getManager();
98
        $entityManager->persist($movie);
99
        $entityManager->flush();
100
101
        return $this->response($movie, 200, [], [
102
            'groups' => ['view'],
103
        ]);
104
    }
105
}