Completed
Push — master ( d80943...13b91b )
by one
04:30
created

MovieController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Controller;
13
14
use App\Entity\Movie;
15
use App\Exception\ApiException;
16
use App\Form\Filter\MovieFilter;
17
use App\Form\MovieType;
18
use App\Interfaces\ControllerInterface;
19
use Nelmio\ApiDocBundle\Annotation\Model;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
21
use Swagger\Annotations as SWG;
22
use Symfony\Component\HttpFoundation\JsonResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\Routing\Annotation\Route;
26
27
/**
28
 * @Route(path="/movies")
29
 */
30
class MovieController extends AbstractController implements ControllerInterface
31
{
32
    /**
33
     * MovieController constructor.
34
     */
35 13
    public function __construct()
36
    {
37 13
        parent::__construct(Movie::class);
38 13
    }
39
40
    /**
41
     * Get all Movies.
42
     *
43
     * @Route(name="api_movie_list", methods={Request::METHOD_GET})
44
     *
45
     * @SWG\Tag(name="Movie")
46
     * @SWG\Response(
47
     *     response=200,
48
     *     description="Returns the list of movies",
49
     *     @SWG\Schema(
50
     *         type="array",
51
     *         @SWG\Items(ref=@Model(type=Movie::class))
52
     *     )
53
     * )
54
     *
55
     * @param Request $request
56
     *
57
     * @return JsonResponse
58
     */
59 2
    public function listAction(Request $request): JsonResponse
60
    {
61 2
        return $this->createCollectionResponse(
62 2
            $this->handleFilterForm(
63 2
                $request,
64 2
                MovieFilter::class
65
            )
66
        );
67
    }
68
69
    /**
70
     * Show single Movies.
71
     *
72
     * @Route(path="/{movie}", name="api_movie_show", methods={Request::METHOD_GET})
73
     *
74
     * @SWG\Tag(name="Movie")
75
     * @SWG\Response(
76
     *     response=200,
77
     *     description="Returns movie of given identifier.",
78
     *     @SWG\Schema(
79
     *         type="object",
80
     *         title="movie",
81
     *         @SWG\Items(ref=@Model(type=Movie::class))
82
     *     )
83
     * )
84
     *
85
     * @param Movie|null $movie
86
     *
87
     * @return JsonResponse
88
     */
89 2
    public function showAction(Movie $movie = null): JsonResponse
90
    {
91 2
        if (false === !!$movie) {
92 1
            return $this->createNotFoundResponse();
93
        }
94
95 1
        return $this->createResourceResponse($movie);
96
    }
97
98
    /**
99
     * Add new Movie.
100
     *
101
     * @Route(name="api_movie_create", methods={Request::METHOD_POST})
102
     *
103
     * @SWG\Tag(name="Movie")
104
     * @SWG\Response(
105
     *     response=200,
106
     *     description="Updates Movie of given identifier and returns the updated object.",
107
     *     @SWG\Schema(
108
     *         type="object",
109
     *         @SWG\Items(ref=@Model(type=Movie::class))
110
     *     )
111
     * )
112
     *
113
     * @param Request $request
114
     * @param Movie|null $movie
115
     *
116
     * @return JsonResponse
117
     *
118
     * @Security("is_granted('CAN_CREATE_MOVIE', movie)")
119
     */
120 2
    public function createAction(Request $request, Movie $movie = null): JsonResponse
121
    {
122 2
        if (false === !!$movie) {
123 2
            $movie = new Movie();
124
        }
125
126 2
        $form = $this->getForm(
127 2
            MovieType::class,
128 2
            $movie,
129
            [
130 2
                'method' => $request->getMethod(),
131
            ]
132
        );
133
134
        try {
135 2
            $this->formHandler->process($request, $form);
136 1
        } catch (ApiException $e) {
137 1
            return new JsonResponse($e->getMessage(), Response::HTTP_BAD_REQUEST);
138
        }
139
140 1
        return $this->createResourceResponse($movie, Response::HTTP_CREATED);
141
    }
142
143
    /**
144
     * Edit existing Movie.
145
     *
146
     * @Route(path="/{movie}", name="api_movie_edit", methods={Request::METHOD_PATCH})
147
     *
148
     * @SWG\Tag(name="Movie")
149
     * @SWG\Response(
150
     *     response=200,
151
     *     description="Updates Movie of given identifier and returns the updated object.",
152
     *     @SWG\Schema(
153
     *         type="object",
154
     *         @SWG\Items(ref=@Model(type=Movie::class))
155
     *     )
156
     * )
157
     *
158
     * @param Request $request
159
     * @param Movie|null $movie
160
     *
161
     * @return JsonResponse
162
     *
163
     * @Security("is_granted('CAN_UPDATE_MOVIE', movie)")
164
     */
165 2
    public function updateAction(Request $request, Movie $movie = null): JsonResponse
166
    {
167 2
        if (false === !!$movie) {
168 1
            return $this->createNotFoundResponse();
169
        }
170
171 1
        $form = $this->getForm(
172 1
            MovieType::class,
173 1
            $movie,
174
            [
175 1
                'method' => $request->getMethod(),
176
            ]
177
        );
178
179
        try {
180 1
            $this->formHandler->process($request, $form);
181
        } catch (ApiException $e) {
182
            return new JsonResponse($e->getMessage(), Response::HTTP_BAD_REQUEST);
183
        }
184
185 1
        return $this->createResourceResponse($movie);
186
    }
187
188
    /**
189
     * Delete Movie.
190
     *
191
     * @Route(path="/{movie}", name="api_movie_delete", methods={Request::METHOD_DELETE})
192
     *
193
     * @SWG\Tag(name="Movie")
194
     * @SWG\Response(
195
     *     response=200,
196
     *     description="Delete Movie of given identifier and returns the empty object.",
197
     *     @SWG\Schema(
198
     *         type="object",
199
     *         @SWG\Items(ref=@Model(type=Movie::class))
200
     *     )
201
     * )
202
     *
203
     * @param Movie|null $movie
204
     *
205
     * @return JsonResponse
206
     *
207
     * @Security("is_granted('CAN_DELETE_MOVIE', movie)")
208
     */
209 2
    public function deleteAction(Movie $movie = null): JsonResponse
210
    {
211 2
        if (false === !!$movie) {
212 1
            return $this->createNotFoundResponse();
213
        }
214
215
        try {
216 1
            $this->entityManager->remove($movie);
217 1
            $this->entityManager->flush();
218
        } catch (\Exception $exception) {
219
            return $this->createGenericErrorResponse($exception);
220
        }
221
222 1
        return $this->createSuccessfulApiResponse(self::DELETED);
223
    }
224
}
225