Completed
Pull Request — master (#29)
by Valentyn
07:01
created

GenreController::postGenres()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
ccs 6
cts 7
cp 0.8571
cc 2
eloc 8
nc 2
nop 3
crap 2.0116
1
<?php
2
3
namespace App\Genres\Controller;
4
5
use App\Controller\ControllerInterface;
6
use App\Genres\Entity\Genre;
7
use App\Users\Entity\User;
8
use App\Genres\Request\CreateGenreRequest;
9
use App\Genres\Request\UpdateGenreRequest;
10
use App\Genres\Service\GenreManageService;
11
use App\Users\Entity\UserRoles;
12
use FOS\RestBundle\Controller\FOSRestController;
13
use Swagger\Annotations as SWG;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Nelmio\ApiDocBundle\Annotation\Model;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
/**
19
 * Class GenreController
20
 * @package App\Genres\Controller
21
 */
22
class GenreController extends FOSRestController implements ControllerInterface
23
{
24
    /**
25
     * Get all genres
26
     *
27
     * @Route("/api/genres", methods={"GET"})
28
     * @SWG\Response(
29
     *     description="REST action which returns all genres.",
30
     *     response=200,
31
     *     @SWG\Schema(
32
     *         type="array",
33
     *         @SWG\Items(ref=@Model(type=Genre::class, groups={"full"}))
34
     *     )
35
     * )
36
     *
37
     * @return array
38 3
     */
39
    public function getAll()
40 3
    {
41
        return $this->getDoctrine()->getRepository(Genre::class)->findAll();
42
43
    }
44
45
    /**
46
     * Create new genre
47
     *
48
     * @Route("/api/genres", methods={"POST"})
49
     * @SWG\Parameter(name="genre[translations][0][locale]", in="formData", type="string")
50
     * @SWG\Parameter(name="genre[translations][0][name]", in="formData", type="string")
51
     * @SWG\Response(
52
     *     description="New genre action.",
53
     *     response=202,
54
     *     @Model(type=Genre::class)
55
     * )
56
     * @param \App\Genres\Request\CreateGenreRequest $request
57
     * @param \App\Genres\Service\GenreManageService $genreManageService
58
     * @param ValidatorInterface $validator
59
     * @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
60 3
     */
61
    public function postGenres(CreateGenreRequest $request, GenreManageService $genreManageService, ValidatorInterface $validator)
62 3
    {
63
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
64 2
65 2
        $genre = $genreManageService->createGenreByRequest($request);
66
        $errors = $validator->validate($genre);
67 2
68
        if (count($errors)) {
69
            return $request->getErrorResponse($errors);
70
        }
71 2
72
        $this->getDoctrine()->getManager()->flush();
73 2
74
        return $genre;
75
    }
76
77
    /**
78
     * Update genre
79
     *
80
     * @Route("/api/genres/{id}", methods={"POST"})
81
     * @SWG\Parameter(name="genre[translations][0][locale]", in="formData", type="string")
82
     * @SWG\Parameter(name="genre[translations][0][name]", in="formData", type="string")
83
     * @SWG\Response(
84
     *     description="New genre action.",
85
     *     response=202,
86
     *     @Model(type=Genre::class)
87
     * )
88
     * @param UpdateGenreRequest $request
89
     * @param Genre $genre
90
     * @param \App\Genres\Service\GenreManageService $genreManageService
91
     * @param ValidatorInterface $validator
92
     * @return Genre|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
93 1
     */
94
    public function putGenres(UpdateGenreRequest $request, Genre $genre, GenreManageService $genreManageService, ValidatorInterface $validator)
95 1
    {
96
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
97 1
98 1
        $genre = $genreManageService->updateGenreByRequest($request, $genre);
99
        $errors = $validator->validate($genre);
100 1
101
        if (count($errors)) {
102
            return $request->getErrorResponse($errors);
103
        }
104 1
105
        $this->getDoctrine()->getManager()->flush();
106 1
107
        return $genre;
108
    }
109
}