Completed
Pull Request — master (#21)
by Valentyn
01:42
created

GenreController::putGenres()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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