Passed
Push — master ( 0ced5e...1a41ca )
by Pierre
03:06
created

Restful::update()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 17
ccs 11
cts 13
cp 0.8462
crap 3.0327
rs 9.8333
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
            $params = $this->request->getParams();
190 1
            $pk = $this->userRepository->getPrimary();
191 1
            if (false === isset($params[$pk])) {
192 1
                throw new \Exception('Missing primary : ' . $pk);
193
            }
194
            $this->userRepository->update($params, [$pk => $params[$pk]]);
195
            $this->sql = $this->userRepository->getSql();
196 1
        } catch (\Exception $e) {
197 1
            $this->error = true;
198 1
            $this->errorMessage = $e->getMessage();
199 1
            $this->sql = '';
200
        }
201 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
202
    }
203
204
    /**
205
     * delete
206
     *
207
     * @OA\Delete(
208
     *     path="/api/v1/restful",
209
     *     summary="Delete something item",
210
     *     @OA\RequestBody(
211
     *         @OA\MediaType(
212
     *             mediaType="application/json",
213
     *             @OA\Schema(
214
     *                 @OA\Property(
215
     *                     property="id",
216
     *                     type="string"
217
     *                 ),
218
     *                 example={"id": 10}
219
     *             )
220
     *         )
221
     *     ),
222
     *     @OA\Response(
223
     *         response=200,
224
     *         description="OK"
225
     *     )
226
     * )
227
     * @return Restful
228
     */
229 1
    final public function delete(): Restful
230
    {
231 1
        $this->bindValues = [];
232
        try {
233 1
            $this->userRepository->delete($this->request->getParams());
234
            $this->sql = $this->userRepository->getSql();
235 1
        } catch (\Exception $e) {
236 1
            $this->error = true;
237 1
            $this->errorMessage = $e->getMessage();
238 1
            $this->sql = '';
239
        }
240 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
241
    }
242
243
    /**
244
     * set response with for a classname and action
245
     *
246
     * @param string $classname
247
     * @param string $action
248
     * @return Restful
249
     */
250 1
    protected function setResponse(string $classname, string $action): Restful
251
    {
252 1
        $this->response
253 1
            ->setCode(
254 1
                ($this->error)
255
                    ?  Response::HTTP_BAD_REQUEST
256 1
                    : Response::HTTP_OK
257
            )
258 1
            ->setContent(
259
                [
260 1
                    'error' => $this->error,
261 1
                    'errorMessage' => $this->errorMessage,
262
                    'datas' => [
263 1
                        'method' => $this->request->getMethod(),
264 1
                        'params' => $this->request->getParams(),
265 1
                        'controller' => $classname,
266 1
                        'action' => $action,
267 1
                        'query' => $this->sql,
268 1
                        'queryValues' => $this->bindValues,
269 1
                        'server' => $_SERVER
270
                    ]
271
                ]
272
            );
273 1
        return $this;
274
    }
275
}
276