Completed
Pull Request — master (#1)
by one
09:53
created

BookController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 36
dl 0
loc 188
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A showAction() 0 7 2
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\Book;
15
use App\Exception\ApiException;
16
use App\Form\BookType;
17
use App\Form\Filter\BookFilter;
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="/books")
29
 */
30
class BookController extends AbstractController implements ControllerInterface
31
{
32
    /**
33
     * BookController constructor.
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct(Book::class);
38
    }
39
40
    /**
41
     * Get all Books.
42
     *
43
     * @Route(name="api_book_list", methods={Request::METHOD_GET})
44
     *
45
     * @SWG\Tag(name="Book")
46
     * @SWG\Response(
47
     *     response=200,
48
     *     description="Returns the list of books.yml",
49
     *     @SWG\Schema(
50
     *         type="array",
51
     *         @SWG\Items(ref=@Model(type=Book::class))
52
     *     )
53
     * )
54
     *
55
     * @param Request $request
56
     *
57
     * @return JsonResponse
58
     */
59
    public function listAction(Request $request): JsonResponse
60
    {
61
        return $this->createCollectionResponse(
62
            $this->handleFilterForm(
63
                $request,
64
                BookFilter::class
65
            )
66
        );
67
    }
68
69
    /**
70
     * Show single Books.
71
     *
72
     * @Route(path="/{book}", name="api_book_show", methods={Request::METHOD_GET})
73
     *
74
     * @SWG\Tag(name="Book")
75
     * @SWG\Response(
76
     *     response=200,
77
     *     description="Returns book of given identifier.",
78
     *     @SWG\Schema(
79
     *         type="object",
80
     *         @SWG\Items(ref=@Model(type=Book::class))
81
     *     )
82
     * )
83
     *
84
     * @param Book|null $book
85
     *
86
     * @return JsonResponse
87
     */
88
    public function showAction(Book $book = null): JsonResponse
89
    {
90
        if (false === !!$book) {
91
            return $this->createNotFoundResponse();
92
        }
93
94
        return $this->createResourceResponse($book);
95
    }
96
97
    /**
98
     * Add new Book.
99
     *
100
     * @Route(name="api_book_create", methods={Request::METHOD_POST})
101
     *
102
     * @SWG\Tag(name="Book")
103
     * @SWG\Response(
104
     *     response=201,
105
     *     description="Creates new Book and returns the created object.",
106
     *     @SWG\Schema(
107
     *         type="object",
108
     *         @SWG\Items(ref=@Model(type=Book::class))
109
     *     )
110
     * )
111
     *
112
     * @param Request $request
113
     * @param Book|null $book
114
     *
115
     * @return JsonResponse
116
     *
117
     * @Security("is_granted('CAN_CREATE_BOOK', book)")
118
     */
119
    public function createAction(Request $request, Book $book = null): JsonResponse
120
    {
121
        if (false === !!$book) {
122
            $book = new Book();
123
        }
124
125
        $form = $this->getForm(
126
            BookType::class,
127
            $book,
128
            [
129
                'method' => $request->getMethod(),
130
            ]
131
        );
132
133
        try {
134
            $this->formHandler->process($request, $form);
135
        } catch (ApiException $e) {
136
            return new JsonResponse($e->getData(), Response::HTTP_BAD_REQUEST);
137
        }
138
139
        return $this->createResourceResponse($book, Response::HTTP_CREATED);
140
    }
141
142
    /**
143
     * Edit existing Book.
144
     *
145
     * @Route(path="/{book}", name="api_book_edit", methods={Request::METHOD_PATCH})
146
     *
147
     * @SWG\Tag(name="Book")
148
     * @SWG\Response(
149
     *     response=200,
150
     *     description="Updates Book of given identifier and returns the updated object.",
151
     *     @SWG\Schema(
152
     *         type="object",
153
     *         @SWG\Items(ref=@Model(type=Book::class))
154
     *     )
155
     * )
156
     *
157
     * @param Request $request
158
     * @param Book|null $book
159
     *
160
     * @return JsonResponse
161
     *
162
     * @Security("is_granted('CAN_UPDATE_BOOK', book)")
163
     */
164
    public function updateAction(Request $request, Book $book = null): JsonResponse
165
    {
166
        if (false === !!$book) {
167
            return $this->createNotFoundResponse();
168
        }
169
170
        $form = $this->getForm(
171
            BookType::class,
172
            $book,
173
            [
174
                'method' => $request->getMethod(),
175
            ]
176
        );
177
178
        try {
179
            $this->formHandler->process($request, $form);
180
        } catch (ApiException $e) {
181
            return new JsonResponse($e->getMessage(), Response::HTTP_BAD_REQUEST);
182
        }
183
184
        return $this->createResourceResponse($book);
185
    }
186
187
    /**
188
     * Delete Book.
189
     *
190
     * @Route(path="/{book}", name="api_book_delete", methods={Request::METHOD_DELETE})
191
     *
192
     * @SWG\Tag(name="Book")
193
     * @SWG\Response(
194
     *     response=200,
195
     *     description="Delete Book of given identifier and returns the empty object.",
196
     *     @SWG\Schema(
197
     *         type="object",
198
     *         @SWG\Items(ref=@Model(type=Book::class))
199
     *     )
200
     * )
201
     *
202
     * @param Book|null $book
203
     *
204
     * @return JsonResponse
205
     *
206
     * @Security("is_granted('CAN_DELETE_BOOK', book)")
207
     */
208
    public function deleteAction(Book $book = null): JsonResponse
209
    {
210
        if (false === !!$book) {
211
            return $this->createNotFoundResponse();
212
        }
213
214
        $this->entityManager->remove($book);
215
        $this->entityManager->flush();
216
217
        return $this->createSuccessfulApiResponse(self::DELETED);
218
    }
219
}
220