Restful   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 320
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 97
c 5
b 0
f 1
dl 0
loc 320
ccs 93
cts 93
cp 1
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 11 2
A setResponse() 0 21 1
A store() 0 22 4
A __construct() 0 8 1
A isError() 0 3 1
A update() 0 25 4
A getStatusCode() 0 5 2
A delete() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controllers\Api\V1;
6
7
use Nymfonya\Component\Container;
8
use Nymfonya\Component\Http\Response;
9
use App\Interfaces\Controllers\IApi;
10
use App\Interfaces\Controllers\IRestful;
11
use App\Reuse\Controllers\AbstractApi;
12
use App\Model\Repository\Users;
13
use App\Component\Db\Core;
14
use OpenApi\Annotations as OA;
15
16
/**
17
 * @OA\Info(
18
 *     version="1.0",
19
 *     title="Api Restful Controller"
20
 * )
21
 */
22
final class Restful extends AbstractApi implements IApi, IRestful
23
{
24
25
    const _ID = 'id';
26
27
    /**
28
     * core db instance
29
     *
30
     * @var Core
31
     */
32
    protected $db;
33
34
    /**
35
     * user repository
36
     *
37
     * @var Users
38
     */
39
    protected $userRepository;
40
41
    /**
42
     * slugs
43
     *
44
     * @var array
45
     */
46
    protected $slugs;
47
48
    /**
49
     * sql
50
     *
51
     * @var String
52
     */
53
    protected $sql;
54
55
    /**
56
     * sql values to bind statement
57
     *
58
     * @var array
59
     */
60
    protected $bindValues;
61
62
    /**
63
     * error
64
     *
65
     * @var Boolean
66
     */
67
    protected $error;
68
69
    /**
70
     * error message
71
     *
72
     * @var String
73
     */
74
    protected $errorMessage;
75
76
    /**
77
     * instanciate
78
     *
79
     * @param Container $container
80
     */
81 11
    public function __construct(Container $container)
82
    {
83 11
        $this->userRepository = new Users($container);
84 11
        $this->db = new Core($container);
85 11
        $this->db->fromOrm($this->userRepository);
86 11
        $this->error = false;
87 11
        $this->errorMessage = '';
88 11
        parent::__construct($container);
89
    }
90
91
    /**
92
     * index
93
     *
94
     * @OA\Get(
95
     *     path="/api/v1/restful/index",
96
     *     summary="Search for a something item",
97
     *     @OA\RequestBody(
98
     *         @OA\MediaType(
99
     *             mediaType="application/json",
100
     *             @OA\Schema(
101
     *                 @OA\Property(
102
     *                     property="id",
103
     *                     type="string"
104
     *                 ),
105
     *                 @OA\Property(
106
     *                     property="name",
107
     *                     type="string"
108
     *                 ),
109
     *                 example={"id": 10}
110
     *             )
111
     *         )
112
     *     ),
113
     *     @OA\Response(
114
     *         response=200,
115
     *         description="OK"
116
     *     )
117
     * )
118
     * @return Restful
119
     */
120 1
    final public function index(array $slugs = []): Restful
121
    {
122 1
        $this->slugs = $slugs;
123 1
        $where = (isset($slugs[self::_ID]))
124 1
            ? [self::_ID => $slugs[self::_ID]]
125 1
            : [];
126 1
        $this->userRepository->find(['*'], $where);
127 1
        $this->sql = $this->userRepository->getSql();
128 1
        $this->bindValues = $this->userRepository->getBuilderValues();
129 1
        $this->db->run($this->sql, $this->bindValues)->hydrate();
130 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
131
    }
132
133
    /**
134
     * store
135
     *
136
     * @OA\Post(
137
     *     path="/api/v1/restful",
138
     *     summary="Adds a new something item",
139
     *     @OA\RequestBody(
140
     *         @OA\MediaType(
141
     *             mediaType="application/json",
142
     *             @OA\Schema(
143
     *                 @OA\Property(
144
     *                     property="id",
145
     *                     type="string"
146
     *                 ),
147
     *                 @OA\Property(
148
     *                     property="name",
149
     *                     type="string"
150
     *                 ),
151
     *                 example={"id": 10, "name": "Jessica Smith"}
152
     *             )
153
     *         )
154
     *     ),
155
     *     @OA\Response(
156
     *         response=200,
157
     *         description="OK"
158
     *     )
159
     * )
160
     * @return Restful
161
     */
162 2
    final public function store(array $slugs = []): Restful
163
    {
164 2
        $this->slugs = $slugs;
165 2
        $this->bindValues = [];
166
        try {
167 2
            $params = $this->getParams();
168 2
            $pk = $this->userRepository->getPrimary();
169 2
            if (isset($params[$pk])) {
170 1
                unset($params[$pk]);
171
            }
172 2
            $this->userRepository->insert($params);
173 1
            $this->sql = $this->userRepository->getSql();
174 1
            $this->bindValues = $this->userRepository->getBuilderValues();
175 1
            if (!empty($params)) {
176 1
                $this->db->run($this->sql, $this->bindValues);
177
            }
178 1
        } catch (\Exception $e) {
179 1
            $this->error = true;
180 1
            $this->errorMessage = $e->getMessage();
181 1
            $this->sql = '';
182
        }
183 2
        return $this->setResponse(__CLASS__, __FUNCTION__);
184
    }
185
186
    /**
187
     * update
188
     *
189
     * @OA\Put(
190
     *     path="/api/v1/restful",
191
     *     summary="Modify something item",
192
     *     @OA\RequestBody(
193
     *         @OA\MediaType(
194
     *             mediaType="application/json",
195
     *             @OA\Schema(
196
     *                 @OA\Property(
197
     *                     property="id",
198
     *                     type="string"
199
     *                 ),
200
     *                 @OA\Property(
201
     *                     property="name",
202
     *                     type="string"
203
     *                 ),
204
     *                 example={"id": 10, "name": "Jessica Smoke"}
205
     *             )
206
     *         )
207
     *     ),
208
     *     @OA\Response(
209
     *         response=200,
210
     *         description="OK"
211
     *     )
212
     * )
213
     * @return Restful
214
     */
215 2
    final public function update(array $slugs = []): Restful
216
    {
217 2
        $this->slugs = $slugs;
218 2
        $this->bindValues = [];
219
        try {
220 2
            $params = $this->getParams();
221 2
            $pk = $this->userRepository->getPrimary();
222 2
            if (isset($slugs[$pk])) {
223 1
                $params[$pk] = $slugs[$pk];
224
            }
225 2
            if (false === isset($params[$pk])) {
226 1
                throw new \Exception('Missing primary : ' . $pk);
227
            }
228 2
            $pkValue = $params[$pk];
229 2
            unset($params[$pk]);
230 2
            $this->userRepository->update($params, [$pk => $pkValue]);
231 1
            $this->sql = $this->userRepository->getSql();
232 1
            $this->bindValues = $this->userRepository->getBuilderValues();
233 1
            $this->db->run($this->sql, $this->bindValues);
234 1
        } catch (\Exception $e) {
235 1
            $this->error = true;
236 1
            $this->errorMessage = $e->getMessage();
237 1
            $this->sql = '';
238
        }
239 2
        return $this->setResponse(__CLASS__, __FUNCTION__);
240
    }
241
242
    /**
243
     * delete
244
     *
245
     * @OA\Delete(
246
     *     path="/api/v1/restful",
247
     *     summary="Delete something item",
248
     *     @OA\RequestBody(
249
     *         @OA\MediaType(
250
     *             mediaType="application/json",
251
     *             @OA\Schema(
252
     *                 @OA\Property(
253
     *                     property="id",
254
     *                     type="string"
255
     *                 ),
256
     *                 example={"id": 10}
257
     *             )
258
     *         )
259
     *     ),
260
     *     @OA\Response(
261
     *         response=200,
262
     *         description="OK"
263
     *     )
264
     * )
265
     * @return Restful
266
     */
267 2
    final public function delete(array $slugs = []): Restful
268
    {
269 2
        $this->slugs = $slugs;
270 2
        $this->bindValues = [];
271
        try {
272 2
            $params = $this->getParams();
273 2
            $pk = $this->userRepository->getPrimary();
274 2
            if (isset($slugs[$pk])) {
275 1
                $params[$pk] = $slugs[$pk];
276
            }
277 2
            if (false === isset($params[$pk])) {
278 1
                throw new \Exception('Missing primary : ' . $pk);
279
            }
280 2
            $this->userRepository->delete([$pk => $params[$pk]]);
281 2
            $this->sql = $this->userRepository->getSql();
282 2
            $this->bindValues = $this->userRepository->getBuilderValues();
283 2
            $this->db->run($this->sql, $this->bindValues);
284 1
        } catch (\Exception $e) {
285 1
            $this->error = true;
286 1
            $this->errorMessage = $e->getMessage();
287 1
            $this->sql = '';
288
        }
289 2
        return $this->setResponse(__CLASS__, __FUNCTION__);
290
    }
291
292
    /**
293
     * set response with for a classname and action
294
     *
295
     * @param string $classname
296
     * @param string $action
297
     * @return Restful
298
     */
299 1
    protected function setResponse(string $classname, string $action): Restful
300
    {
301 1
        $this->response
302 1
            ->setCode($this->getStatusCode())
303 1
            ->setContent(
304
                [
305 1
                    'error' => $this->error,
306 1
                    'errorMessage' => $this->errorMessage,
307
                    'datas' => [
308 1
                        'method' => $this->getRequest()->getMethod(),
309 1
                        'params' => $this->getParams(),
310 1
                        'controller' => $classname,
311 1
                        'action' => $action,
312 1
                        'query' => $this->sql,
313 1
                        'queryValues' => $this->bindValues,
314 1
                        'slugs' => $this->slugs,
315 1
                        'rowset' => $this->db->getRowset()
316
                    ]
317
                ]
318
            );
319 1
        return $this;
320
    }
321
322
    /**
323
     * returns true if error happened
324
     *
325
     * @return boolean
326
     */
327 7
    protected function isError(): bool
328
    {
329 7
        return $this->error === true;
330
    }
331
332
    /**
333
     * returns http status code
334
     *
335
     * @return int
336
     */
337 7
    protected function getStatusCode(): int
338
    {
339 7
        return (true === $this->isError())
340 3
            ? Response::HTTP_BAD_REQUEST
341 7
            : Response::HTTP_OK;
342
    }
343
}
344