Passed
Push — master ( 225f0a...b074c6 )
by Pierre
02:53
created

Restful::store()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 13
ccs 8
cts 10
cp 0.8
crap 2.032
rs 9.9332
1
<?php
2
3
namespace App\Controllers\Api\V1;
4
5
use App\Interfaces\Controllers\IApi;
6
use App\Reuse\Controllers\AbstractApi;
7
use App\Http\Response;
8
use App\Container;
9
use App\Model\Repository\Users;
10
use OpenApi\Annotations as OA;
11
12
/**
13
 * @OA\Info(
14
 *     version="1.0",
15
 *     title="Api Restful Controller"
16
 * )
17
 */
18
final class Restful extends AbstractApi implements IApi
19
{
20
21
    /**
22
     * user repository
23
     *
24
     * @var Users
25
     */
26
    protected $userRepository;
27
28
    /**
29
     * sql
30
     *
31
     * @var String
32
     */
33
    protected $sql;
34
35
    /**
36
     * sql values to bind statement
37
     *
38
     * @var array
39
     */
40
    protected $bindValues;
41
42
    /**
43
     * error
44
     *
45
     * @var Boolean
46
     */
47
    protected $error;
48
49
   /**
50
     * error message
51
     *
52
     * @var String
53
     */
54
    protected $errorMessage;
55
56
    /**
57
     * instanciate
58
     *
59
     * @param Container $container
60
     */
61 6
    public function __construct(Container $container)
62
    {
63 6
        $this->userRepository = new Users($container);
64 6
        $this->error = false;
65 6
        $this->errorMessage = '';
66 6
        parent::__construct($container);
67
    }
68
69
    /**
70
     * index
71
     *
72
     * @OA\Get(
73
     *     path="/api/v1/restful/index",
74
     *     summary="Search for a something item",
75
     *     @OA\RequestBody(
76
     *         @OA\MediaType(
77
     *             mediaType="application/json",
78
     *             @OA\Schema(
79
     *                 @OA\Property(
80
     *                     property="id",
81
     *                     type="string"
82
     *                 ),
83
     *                 @OA\Property(
84
     *                     property="name",
85
     *                     type="string"
86
     *                 ),
87
     *                 example={"id": 10}
88
     *             )
89
     *         )
90
     *     ),
91
     *     @OA\Response(
92
     *         response=200,
93
     *         description="OK"
94
     *     )
95
     * )
96
     * @return Restful
97
     */
98 1
    final public function index(): Restful
99
    {
100 1
        $this->userRepository->find(
101 1
            ['name'],
102
            [
103 1
                'name' => ['john', 'elisa'],
104
                'job>' => 0
105
            ]
106
        );
107 1
        $this->sql = $this->userRepository->getSql();
108 1
        $this->bindValues = $this->userRepository->getBuilderValues();
109 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
110
    }
111
112
    /**
113
     * store
114
     *
115
     * @OA\Post(
116
     *     path="/api/v1/restful",
117
     *     summary="Adds a new something item",
118
     *     @OA\RequestBody(
119
     *         @OA\MediaType(
120
     *             mediaType="application/json",
121
     *             @OA\Schema(
122
     *                 @OA\Property(
123
     *                     property="id",
124
     *                     type="string"
125
     *                 ),
126
     *                 @OA\Property(
127
     *                     property="name",
128
     *                     type="string"
129
     *                 ),
130
     *                 example={"id": 10, "name": "Jessica Smith"}
131
     *             )
132
     *         )
133
     *     ),
134
     *     @OA\Response(
135
     *         response=200,
136
     *         description="OK"
137
     *     )
138
     * )
139
     * @return Restful
140
     */
141 1
    final public function store(): Restful
142
    {
143 1
        $this->bindValues = [];
144
        try {
145 1
            $this->userRepository->insert($this->request->getParams());
146
            $this->sql = $this->userRepository->getSql();
147
            $this->bindValues = $this->userRepository->getBuilderValues();
148 1
        } catch (\Exception $e) {
149 1
            $this->error = true;
150 1
            $this->errorMessage = $e->getMessage();
151 1
            $this->sql = '';
152
        }
153 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
154
    }
155
156
    /**
157
     * update
158
     *
159
     * @OA\Put(
160
     *     path="/api/v1/restful",
161
     *     summary="Modify something item",
162
     *     @OA\RequestBody(
163
     *         @OA\MediaType(
164
     *             mediaType="application/json",
165
     *             @OA\Schema(
166
     *                 @OA\Property(
167
     *                     property="id",
168
     *                     type="string"
169
     *                 ),
170
     *                 @OA\Property(
171
     *                     property="name",
172
     *                     type="string"
173
     *                 ),
174
     *                 example={"id": 10, "name": "Jessica Smoke"}
175
     *             )
176
     *         )
177
     *     ),
178
     *     @OA\Response(
179
     *         response=200,
180
     *         description="OK"
181
     *     )
182
     * )
183
     * @return Restful
184
     */
185 1
    final public function update(): Restful
186
    {
187 1
        $this->bindValues = [];
188
        try {
189 1
            $this->userRepository->update($this->request->getParams());
190
            $this->sql = $this->userRepository->getSql();
191 1
        } catch (\Exception $e) {
192 1
            $this->error = true;
193 1
            $this->errorMessage = $e->getMessage();
194 1
            $this->sql = '';
195
        }
196 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
197
    }
198
199
    /**
200
     * delete
201
     *
202
     * @OA\Delete(
203
     *     path="/api/v1/restful",
204
     *     summary="Delete something item",
205
     *     @OA\RequestBody(
206
     *         @OA\MediaType(
207
     *             mediaType="application/json",
208
     *             @OA\Schema(
209
     *                 @OA\Property(
210
     *                     property="id",
211
     *                     type="string"
212
     *                 ),
213
     *                 example={"id": 10}
214
     *             )
215
     *         )
216
     *     ),
217
     *     @OA\Response(
218
     *         response=200,
219
     *         description="OK"
220
     *     )
221
     * )
222
     * @return Restful
223
     */
224 1
    final public function delete(): Restful
225
    {
226 1
        $this->bindValues = [];
227
        try {
228 1
            $this->userRepository->delete($this->request->getParams());
229
            $this->sql = $this->userRepository->getSql();
230 1
        } catch (\Exception $e) {
231 1
            $this->error = true;
232 1
            $this->errorMessage = $e->getMessage();
233 1
            $this->sql = '';
234
        }
235 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
236
    }
237
238
    /**
239
     * set response with for a classname and action
240
     *
241
     * @param string $classname
242
     * @param string $action
243
     * @return Restful
244
     */
245 1
    protected function setResponse(string $classname, string $action): Restful
246
    {
247 1
        $this->response
248 1
            ->setCode(
249 1
                ($this->error)
250
                    ?  Response::HTTP_BAD_REQUEST
251 1
                    : Response::HTTP_OK
252
            )
253 1
            ->setContent(
254
                [
255 1
                    'error' => $this->error,
256 1
                    'errorMessage' => $this->errorMessage,
257
                    'datas' => [
258 1
                        'method' => $this->request->getMethod(),
259 1
                        'params' => $this->request->getParams(),
260 1
                        'controller' => $classname,
261 1
                        'action' => $action,
262 1
                        'query' => $this->sql,
263 1
                        'queryValues' => $this->bindValues
264
                    ]
265
                ]
266
            );
267 1
        return $this;
268
    }
269
}
270