Completed
Push — master ( d15cf4...5163a0 )
by Valentyn
06:05
created

GenreController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 72
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 7 1
A postGenres() 0 17 2
A putGenres() 0 17 2
1
<?php
2
3
namespace App\Genres\Controller;
4
5
use App\Controller\BaseController;
6
use App\Genres\Entity\Genre;
7
use App\Genres\Request\CreateGenreRequest;
8
use App\Genres\Request\UpdateGenreRequest;
9
use App\Genres\Service\GenreManageService;
10
use App\Users\Entity\UserRoles;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\Routing\Annotation\Route;
13
use Symfony\Component\Validator\Validator\ValidatorInterface;
14
15
/**
16
 * Class GenreController
17
 * @package App\Genres\Controller
18
 */
19
class GenreController extends BaseController
20
{
21
    /**
22
     * Get all genres
23
     *
24
     * @Route("/api/genres", methods={"GET"})
25
     *
26
     * @return JsonResponse
27
     */
28 5
    public function getAll()
29
    {
30 5
        $genres = $this->getDoctrine()->getRepository(Genre::class)->findAll();
31 5
        return $this->response($genres, 200, [], [
32 5
            'groups' => ['list'],
33
        ]);
34
    }
35
36
    /**
37
     * Create new genre
38
     *
39
     * @Route("/api/genres", methods={"POST"})
40
     * @param \App\Genres\Request\CreateGenreRequest $request
41
     * @param \App\Genres\Service\GenreManageService $genreManageService
42
     * @param ValidatorInterface $validator
43
     * @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
44
     */
45 2
    public function postGenres(CreateGenreRequest $request, GenreManageService $genreManageService, ValidatorInterface $validator)
46
    {
47 2
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
48
49 1
        $genre = $genreManageService->createGenreByRequest($request);
50 1
        $errors = $validator->validate($genre);
51
52 1
        if (count($errors)) {
53
            return $request->getErrorResponse($errors);
54
        }
55
56 1
        $this->getDoctrine()->getManager()->flush();
57
58 1
        return $this->response($genre, 200, [], [
59 1
            'groups' => ['view'],
60
        ]);
61
    }
62
63
    /**
64
     * Update genre
65
     *
66
     * @Route("/api/genres/{id}", methods={"POST"})
67
     * @param UpdateGenreRequest $request
68
     * @param Genre $genre
69
     * @param \App\Genres\Service\GenreManageService $genreManageService
70
     * @param ValidatorInterface $validator
71
     * @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
72
     */
73 1
    public function putGenres(UpdateGenreRequest $request, Genre $genre, GenreManageService $genreManageService, ValidatorInterface $validator)
74
    {
75 1
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
76
77 1
        $genre = $genreManageService->updateGenreByRequest($request, $genre);
78 1
        $errors = $validator->validate($genre);
79
80 1
        if (count($errors)) {
81
            return $request->getErrorResponse($errors);
82
        }
83
84 1
        $this->getDoctrine()->getManager()->flush();
85
86 1
        return $this->response($genre, 200, [], [
87 1
            'groups' => ['view'],
88
        ]);
89
    }
90
}