Passed
Branch master (ecaff5)
by Joao
02:24
created

Sample::postDummy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace RestTemplate\Rest;
4
5
use Builder\Psr11;
6
use ByJG\Config\Exception\ConfigNotFoundException;
7
use ByJG\Config\Exception\EnvironmentException;
8
use ByJG\Config\Exception\KeyNotFoundException;
9
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException;
10
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
11
use ByJG\RestServer\Exception\Error404Exception;
12
use ByJG\RestServer\HttpRequest;
13
use ByJG\RestServer\HttpResponse;
14
use ByJG\Serializer\BinderObject;
15
use Psr\SimpleCache\InvalidArgumentException;
16
use ReflectionException;
17
use RestTemplate\Model\Dummy;
18
use RestTemplate\Model\DummyHex;
19
use RestTemplate\Repository\DummyHexRepository;
20
use RestTemplate\Repository\DummyRepository;
21
use Swagger\Annotations as SWG;
22
23
class Sample extends ServiceAbstractBase
24
{
25
    /**
26
     * Simple ping
27
     *
28
     * @SWG\Get(
29
     *     path="/sample/ping",
30
     *     tags={"zz_sample"},
31
     *     @SWG\Response(
32
     *         response=200,
33
     *         description="The object",
34
     *         @SWG\Schema(
35
     *            required={"result"},
36
     *            @SWG\Property(property="result", type="string")
37
     *         )
38
     *     )
39
     * )
40
     * @param HttpResponse $response
41
     * @param HttpRequest $request
42
     */
43
    public function getPing($response, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
44
    {
45
        $response->write([
46
            'result' => 'pong'
47
        ]);
48
    }
49
50
    /**
51
     * Get the rows from the Dummy table (used in the example)
52
     * @SWG\Get(
53
     *     path="/sample/dummy/{field}",
54
     *     tags={"zz_sample"},
55
     *     @SWG\Parameter(
56
     *         name="field",
57
     *         in="path",
58
     *         description="The field content to be returned",
59
     *         required=true,
60
     *         type="string"
61
     *     ),
62
     *     @SWG\Response(
63
     *         response=200,
64
     *         description="The object",
65
     *         @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Dummy"))
66
     *     ),
67
     *     @SWG\Response(
68
     *         response=404,
69
     *         description="Not found",
70
     *         @SWG\Schema(ref="#/definitions/error")
71
     *     ),
72
     *     @SWG\Response(
73
     *         response=500,
74
     *         description="Erro Geral",
75
     *         @SWG\Schema(ref="#/definitions/error")
76
     *     )
77
     * )
78
     *
79
     * @param HttpResponse $response
80
     * @param HttpRequest $request
81
     * @throws ConfigNotFoundException
82
     * @throws EnvironmentException
83
     * @throws Error404Exception
84
     * @throws InvalidArgumentException
85
     * @throws KeyNotFoundException
86
     * @throws ReflectionException
87
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
88
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
89
     */
90 View Code Duplication
    public function getDummy($response, $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $dummyRepo = Psr11::container()->get(DummyRepository::class);
93
        $field = $request->param('field');
94
95
        $result = $dummyRepo->getByField($field);
96
        if (empty($result)) {
97
            throw new Error404Exception('Id not found');
98
        }
99
        $response->write(
100
            $result
101
        );
102
    }
103
104
    /**
105
     * Save data content in the table Dummy
106
     * @SWG\Post(
107
     *     path="/sample/dummy",
108
     *     tags={"zz_sample"},
109
     *     @SWG\Parameter(
110
     *         name="body",
111
     *         in="body",
112
     *         description="The dummy data",
113
     *         required=true,
114
     *         @SWG\Schema(ref="#/definitions/Dummy")
115
     *     ),
116
     *     @SWG\Response(
117
     *         response=200,
118
     *         description="The object",
119
     *     ),
120
     *     @SWG\Response(
121
     *         response=500,
122
     *         description="Erro Geral",
123
     *         @SWG\Schema(ref="#/definitions/error")
124
     *     )
125
     * )
126
     *
127
     * @param HttpResponse $response
128
     * @param HttpRequest $request
129
     * @throws ConfigNotFoundException
130
     * @throws EnvironmentException
131
     * @throws InvalidArgumentException
132
     * @throws KeyNotFoundException
133
     * @throws OrmBeforeInvalidException
134
     * @throws OrmInvalidFieldsException
135
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
136
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
137
     * @throws ReflectionException
138
     */
139
    public function postDummy($response, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

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

Loading history...
140
    {
141
        $model = new Dummy();
142
        $payload = json_decode($request->payload());
143
        BinderObject::bindObject($payload, $model);
144
145
        $dummyRepo = Psr11::container()->get(DummyRepository::class);
146
        $dummyRepo->save($model);
147
    }
148
149
    /**
150
     * Get the rows from the DummyHex table by ID
151
     * @SWG\Get(
152
     *     path="/sample/dummyhex/{id}",
153
     *     tags={"zz_sample"},
154
     *     @SWG\Parameter(
155
     *         name="id",
156
     *         in="path",
157
     *         description="The field content to be returned",
158
     *         required=true,
159
     *         type="string"
160
     *     ),
161
     *     @SWG\Response(
162
     *         response=200,
163
     *         description="The object",
164
     *         @SWG\Schema(ref="#/definitions/DummyHex")
165
     *     ),
166
     *     @SWG\Response(
167
     *         response=404,
168
     *         description="Not found",
169
     *         @SWG\Schema(ref="#/definitions/error")
170
     *     ),
171
     *     @SWG\Response(
172
     *         response=500,
173
     *         description="Erro Geral",
174
     *         @SWG\Schema(ref="#/definitions/error")
175
     *     )
176
     * )
177
     *
178
     * @param HttpResponse $response
179
     * @param HttpRequest $request
180
     * @throws ConfigNotFoundException
181
     * @throws EnvironmentException
182
     * @throws Error404Exception
183
     * @throws InvalidArgumentException
184
     * @throws KeyNotFoundException
185
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
186
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
187
     * @throws ReflectionException
188
     */
189 View Code Duplication
    public function getDummyHex($response, $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        $dummyRepo = Psr11::container()->get(DummyHexRepository::class);
192
        $id = $request->param('id');
193
194
        $result = $dummyRepo->get($id);
195
        if (empty($result)) {
196
            throw new Error404Exception('Id not found');
197
        }
198
        $response->write(
199
            $result
200
        );
201
    }
202
203
    /**
204
     * Save data content in the table Dummy Hex
205
     * @SWG\Post(
206
     *     path="/sample/dummyhex",
207
     *     tags={"zz_sample"},
208
     *     @SWG\Parameter(
209
     *         name="body",
210
     *         in="body",
211
     *         description="The dummy data",
212
     *         required=true,
213
     *         @SWG\Schema(ref="#/definitions/DummyHex")
214
     *     ),
215
     *     @SWG\Response(
216
     *         response=200,
217
     *         description="The object",
218
     *         @SWG\Schema(ref="#/definitions/DummyHex")
219
     *     ),
220
     *     @SWG\Response(
221
     *         response=500,
222
     *         description="Erro Geral",
223
     *         @SWG\Schema(ref="#/definitions/error")
224
     *     )
225
     * )
226
     *
227
     * @param HttpResponse $response
228
     * @param HttpRequest $request
229
     * @throws ConfigNotFoundException
230
     * @throws EnvironmentException
231
     * @throws InvalidArgumentException
232
     * @throws KeyNotFoundException
233
     * @throws OrmBeforeInvalidException
234
     * @throws OrmInvalidFieldsException
235
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
236
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
237
     * @throws ReflectionException
238
     */
239
    public function postDummyHex($response, $request)
240
    {
241
        $model = new DummyHex();
242
        $payload = json_decode($request->payload());
243
        BinderObject::bindObject($payload, $model);
244
245
        $dummyRepo = Psr11::container()->get(DummyHexRepository::class);
246
        $dummyRepo->save($model);
247
248
        $model = $dummyRepo->get($model->getId());
249
250
        $response->write($model);
251
    }
252
}
253