BackendController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 183
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 3 1
A list() 0 11 1
A create() 0 21 2
A update() 0 24 2
A delete() 0 6 1
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Document\Backend;
6
use Doctrine\ODM\MongoDB\DocumentManager;
7
use Nelmio\ApiDocBundle\Annotation\Model;
8
use Nelmio\ApiDocBundle\Annotation\Security;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
10
use Swagger\Annotations as SWG;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\Serializer\SerializerInterface;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
/**
19
 * Provide routes for backend API.
20
 *
21
 * @Route("/api/backends", name="api_backend_", defaults={"_format": "json"})
22
 * @IsGranted("ROLE_ADMIN")
23
 */
24
class BackendController extends AbstractController
25
{
26
    /**
27
     * List backend.
28
     *
29
     * @Route("", name="index", methods={"GET"})
30
     * @SWG\Response(
31
     *     response=200,
32
     *     description="Returns the backend",
33
     *     @SWG\Schema(
34
     *         type="array",
35
     *         @SWG\Items(ref=@Model(type=Backend::class, groups={"backend_default"}))
36
     *     )
37
     * )
38
     * @SWG\Parameter(
39
     *     name="page",
40
     *     in="query",
41
     *     type="integer",
42
     *     description="Page number",
43
     *     default="1"
44
     * )
45
     * @SWG\Parameter(
46
     *     name="query",
47
     *     in="query",
48
     *     type="string",
49
     *     description="Search query",
50
     * )
51
     * @SWG\Tag(name="backend")
52
     * @Security(name="Bearer")
53
     */
54
    public function list(DocumentManager $dm, Request $request): Response
55
    {
56
        $page = $request->query->getInt('page', 1);
57
        $query = $request->query->get('query');
58
59
        $rs = [
60
            'total' => $dm->getRepository('App:Backend')->count($query),
61
            'items' => $dm->getRepository('App:Backend')->search($page, $query, 20),
62
        ];
63
64
        return $this->json($rs, Response::HTTP_OK, [], ['groups' => ['backend_default']]);
65
    }
66
67
    /**
68
     * Get a backend.
69
     *
70
     * @Route("/{id}", name="get", methods={"GET"})
71
     * @SWG\Response(
72
     *     response=200,
73
     *     description="Returns the backend",
74
     *     @SWG\Schema(ref=@Model(type=Backend::class, groups={"backend_full"})
75
     *     )
76
     * )
77
     * @SWG\Tag(name="backend")
78
     * @Security(name="Bearer")
79
     */
80
    public function find(Backend $backend): Response
81
    {
82
        return $this->json($backend, Response::HTTP_OK, [], ['groups' => ['backend_full']]);
83
    }
84
85
    /**
86
     * Create a backend.
87
     *
88
     * @Route("", name="create", methods={"POST"})
89
     * @SWG\Response(
90
     *     response=201,
91
     *     description="Create a backend"
92
     * )
93
     * @SWG\Response(
94
     *     response=400,
95
     *     description="Invalid backend",
96
     *     @SWG\Schema(
97
     *         type="object",
98
     *         ref=@Model(type=Backend::class, groups={"backend_full"})
99
     *     )
100
     * )
101
     * @SWG\Parameter(
102
     *     name="body",
103
     *     in="body",
104
     *     description="JSON Payload",
105
     *     required=true,
106
     *     format="application/json",
107
     *     @SWG\Schema(
108
     *         type="object",
109
     *         ref=@Model(type=Backend::class, groups={"backend_write"})
110
     *     )
111
     * )
112
     * @SWG\Tag(name="backend")
113
     * @Security(name="Bearer")
114
     */
115
    public function create(
116
        DocumentManager $dm,
117
        SerializerInterface $serializer,
118
        ValidatorInterface $validator,
119
        Request $request): Response
120
    {
121
        $data = $request->getContent();
122
123
        /** @var Backend $backend */
124
        $backend = $serializer->deserialize($data, Backend::class, 'json', ['groups' => ['backend_write']]);
125
126
        $violations = $validator->validate($backend);
127
128
        if ($violations->count() > 0) {
129
            return $this->json($violations, Response::HTTP_BAD_REQUEST);
130
        }
131
132
        $dm->persist($backend);
133
        $dm->flush();
134
135
        return $this->json($backend, Response::HTTP_CREATED, [], ['groups' => ['backend_full']]);
136
    }
137
138
    /**
139
     * update a backend.
140
     *
141
     * @Route("/{id}", name="update", methods={"PUT"})
142
     * @SWG\Response(
143
     *     response=204,
144
     *     description="Update a backend"
145
     * )
146
     * @SWG\Response(
147
     *     response=400,
148
     *     description="Invalid backend"
149
     * )
150
     * @SWG\Parameter(
151
     *     name="body",
152
     *     in="body",
153
     *     description="JSON Payload",
154
     *     required=true,
155
     *     format="application/json",
156
     *     @SWG\Schema(
157
     *         type="object",
158
     *         ref=@Model(type=Backend::class, groups={"backend_write"})
159
     *     )
160
     * )
161
     * @SWG\Tag(name="backend")
162
     * @Security(name="Bearer")
163
     */
164
    public function update(
165
        Backend $backend,
166
        DocumentManager $dm,
167
        SerializerInterface $serializer,
168
        ValidatorInterface $validator,
169
        Request $request): Response
170
    {
171
        $data = $request->getContent();
172
173
        /* @var Backend $backend */
174
        $serializer->deserialize($data, Backend::class, 'json', [
175
            'groups' => ['backend_write'],
176
            'object_to_populate' => $backend,
177
        ]);
178
179
        $violations = $validator->validate($backend);
180
181
        if ($violations->count() > 0) {
182
            return $this->json($violations, Response::HTTP_BAD_REQUEST);
183
        }
184
185
        $dm->flush();
186
187
        return new Response('', Response::HTTP_NO_CONTENT);
188
    }
189
190
    /**
191
     * Delete a backend.
192
     *
193
     * @Route("/{id}", name="delete", methods={"DELETE"})
194
     * @SWG\Response(
195
     *     response=204,
196
     *     description="Delete a backend"
197
     * )
198
     * @SWG\Tag(name="backend")
199
     * @Security(name="Bearer")
200
     */
201
    public function delete(DocumentManager $dm, Backend $backend): Response
202
    {
203
        $dm->remove($backend);
204
        $dm->flush();
205
206
        return new Response('', Response::HTTP_NO_CONTENT);
207
    }
208
}
209