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

GenreController::postGenres()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 3
crap 2.0054
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
}