Passed
Push — master ( ecaff5...98df6c )
by Joao
13:38 queued 15s
created

DummyRest::putDummy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace RestTemplate\Rest;
4
5
use ByJG\MicroOrm\Literal;
6
use ByJG\RestServer\Exception\Error401Exception;
7
use ByJG\RestServer\Exception\Error404Exception;
8
use ByJG\RestServer\HttpRequest;
9
use ByJG\RestServer\HttpResponse;
10
use ByJG\Serializer\BinderObject;
11
use RestTemplate\Psr11;
12
use RestTemplate\Model\Dummy;
13
use RestTemplate\Repository\DummyRepository;
14
use OpenApi\Annotations as OA;
15
use RestTemplate\Util\HexUuidLiteral;
16
17
class DummyRest extends ServiceAbstractBase
18
{
19
    /**
20
     * Get the Dummy by id
21
     * @OA\Get(
22
     *     path="/dummy/{id}",
23
     *     tags={"Dummy"},
24
     *     security={{
25
     *         "jwt-token":{}
26
     *     }},
27
     *     @OA\Parameter(
28
     *         name="id",
29
     *         description="",
30
     *         in="path",
31
     *         required=true,
32
     *         @OA\Schema(
33
     *             type="integer",
34
     *             format="int32"
35
     *         ) 
36
     *     ),
37
     *     @OA\Response(
38
     *         response=200,
39
     *         description="The object Dummy",
40
     *         @OA\JsonContent(ref="#/components/schemas/Dummy")
41
     *     ),
42
     *     @OA\Response(
43
     *         response=401,
44
     *         description="Not Authorized",
45
     *         @OA\JsonContent(ref="#/components/schemas/error")
46
     *     )
47
     * )
48
     *
49
     * @param HttpResponse $response
50
     * @param HttpRequest $request
51
     * @throws Error401Exception
52
     * @throws InvalidArgumentException
53
     */
54
    public function getDummy($response, $request)
55
    {
56
        $data = $this->requireAuthenticated();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
57
58
        $dummyRepo = Psr11::container()->get(DummyRepository::class);
59
        $id = $request->param('id');
60
61
        $result = $dummyRepo->get($id);
62
        if (empty($result)) {
63
            throw new Error404Exception('Id not found');
64
        }
65
        $response->write(
66
            $result
67
        );
68
    }
69
70
    /**
71
     * List Dummy
72
     * @OA\Get(
73
     *    path="/dummy",
74
     *    tags={"Dummy"},
75
     *    security={{
76
     *       "jwt-token":{}
77
     *    }},
78
     *    @OA\Parameter(
79
     *       name="page",
80
     *       description="Page number",
81
     *       in="query",
82
     *       required=false,
83
     *       @OA\Schema(
84
     *          type="integer"
85
     *       )
86
     *    ),
87
     *    @OA\Parameter(
88
     *       name="size",
89
     *       description="Page size",
90
     *       in="query",
91
     *       required=false,
92
     *       @OA\Schema(
93
     *          type="integer"
94
     *       )
95
     *    ),
96
     *    @OA\Parameter(
97
     *       name="orderBy",
98
     *       description="Order by",
99
     *       in="query",
100
     *       required=false,
101
     *       @OA\Schema(
102
     *          type="string"
103
     *       )
104
     *    ),
105
     *    @OA\Parameter(
106
     *       name="filter",
107
     *       description="Filter",
108
     *       in="query",
109
     *       required=false,
110
     *       @OA\Schema(
111
     *          type="string"
112
     *       )
113
     *    ),
114
     *    @OA\Response(
115
     *      response=200,
116
     *      description="The list of Dummy",
117
     *      @OA\JsonContent(
118
     *         type="array",
119
     *         @OA\Items(ref="#/components/schemas/Dummy")
120
     *      )
121
     *    ),
122
     *    @OA\Response(
123
     *      response=401,
124
     *      description="Not Authorized",
125
     *      @OA\JsonContent(ref="#/components/schemas/error")
126
     *    )
127
     * )
128
     * 
129
     * @param mixed $response
130
     * @param mixed $request
131
     * @return void
132
     */
133
    public function listDummy($response, $request)
134
    {
135
        $data = $this->requireAuthenticated();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
136
137
        $repo = Psr11::container()->get(DummyRepository::class);
138
139
        $page = $request->get('page');
140
        $size = $request->get('size');
141
        // $orderBy = $request->get('orderBy');
142
        // $filter = $request->get('filter');
143
144
        $result = $repo->list($page, $size);
145
        $response->write(
146
            $result
147
        );
148
    }
149
150
151
    /**
152
     * Create a new Dummy 
153
     * @OA\Post(
154
     *     path="/dummy",
155
     *     tags={"Dummy"},
156
     *     security={{
157
     *         "jwt-token":{}
158
     *     }},
159
     *     @OA\RequestBody(
160
     *         description="The object Dummy to be created",
161
     *         required=true,
162
     *         @OA\MediaType(
163
     *           mediaType="application/json",
164
     *           @OA\Schema(
165
     *             
166
167
     *             @OA\Property(property="field", type="string", format="string", nullable=true)
168
     *           )
169
     *         )
170
     *     ),
171
     *     @OA\Response(
172
     *         response=200,
173
     *         description="The id of the object created",
174
     *         @OA\MediaType(
175
     *           mediaType="application/json",
176
     *           @OA\Schema(
177
     *             required={ "id" },
178
179
     *             @OA\Property(property="id", type="integer", format="int32")
180
     *           )
181
     *         )
182
     *     ),
183
     *     @OA\Response(
184
     *         response=401,
185
     *         description="Not Authorized",
186
     *         @OA\JsonContent(ref="#/components/schemas/error")
187
     *     )
188
     * )
189
     *
190
     * @param HttpResponse $response
191
     * @param HttpRequest $request
192
     * @throws Error401Exception
193
     * @throws InvalidArgumentException
194
     */
195
    public function postDummy($response, $request)
196
    {
197
        $data = $this->requireRole("admin");
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
198
199
        $payload = $this->validateRequest($request);
200
        
201
        $model = new Dummy();
202
        BinderObject::bind($payload, $model);
203
204
        $dummyRepo = Psr11::container()->get(DummyRepository::class);
205
        $dummyRepo->save($model);
206
207
        $response->write([ "id" => $model->getId()]);
208
    }
209
210
211
    /**
212
     * Update an existing Dummy 
213
     * @OA\Put(
214
     *     path="/dummy",
215
     *     tags={"Dummy"},
216
     *     security={{
217
     *         "jwt-token":{}
218
     *     }},
219
     *     @OA\RequestBody(
220
     *         description="The object Dummy to be updated",
221
     *         required=true,
222
     *         @OA\JsonContent(ref="#/components/schemas/Dummy")
223
     *     ),
224
     *     @OA\Response(
225
     *         response=200,
226
     *         description="Nothing to return"
227
     *     ),
228
     *     @OA\Response(
229
     *         response=401,
230
     *         description="Not Authorized",
231
     *         @OA\JsonContent(ref="#/components/schemas/error")
232
     *     )
233
     * )
234
     *
235
     * @param HttpResponse $response
236
     * @param HttpRequest $request
237
     * @throws Error401Exception
238
     * @throws InvalidArgumentException
239
     */
240
    public function putDummy($response, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

240
    public function putDummy(/** @scrutinizer ignore-unused */ $response, $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
241
    {
242
        $data = $this->requireRole("admin");
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
243
244
        $payload = $this->validateRequest($request);
245
246
        $dummyRepo = Psr11::container()->get(DummyRepository::class);
247
        $model = $dummyRepo->get($payload['id']);
248
        if (empty($model)) {
249
            throw new Error404Exception('Id not found');
250
        }
251
        BinderObject::bind($payload, $model);
252
253
        $dummyRepo->save($model);
254
    }
255
256
}
257