Completed
Push — master ( b6e546...9a6f5b )
by Pierre
02:45
created

Restful::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 OpenApi\Annotations as OA;
10
11
/**
12
 * @OA\Info(
13
 *     version="1.0",
14
 *     title="Api Restful Controller"
15
 * )
16
 */
17
final class Restful extends AbstractApi implements IApi
18
{
19
20
    /**
21
     * instanciate
22
     *
23
     * @param Container $container
24
     */
25 6
    public function __construct(Container $container)
26
    {
27 6
        parent::__construct($container);
28
    }
29
30
    /**
31
     * index
32
     *
33
     * @OA\Get(
34
     *     path="/api/v1/restful",
35
     *     summary="Search for a something item",
36
     *     @OA\RequestBody(
37
     *         @OA\MediaType(
38
     *             mediaType="application/json",
39
     *             @OA\Schema(
40
     *                 @OA\Property(
41
     *                     property="id",
42
     *                     type="string"
43
     *                 ),
44
     *                 @OA\Property(
45
     *                     property="name",
46
     *                     type="string"
47
     *                 ),
48
     *                 example={"id": 10}
49
     *             )
50
     *         )
51
     *     ),
52
     *     @OA\Response(
53
     *         response=200,
54
     *         description="OK"
55
     *     )
56
     * )
57
     * @return Restful
58
     */
59 1
    final public function index(): Restful
60
    {
61 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
62
    }
63
64
    /**
65
     * store
66
     *
67
     * @OA\Post(
68
     *     path="/api/v1/restful",
69
     *     summary="Adds a new something item",
70
     *     @OA\RequestBody(
71
     *         @OA\MediaType(
72
     *             mediaType="application/json",
73
     *             @OA\Schema(
74
     *                 @OA\Property(
75
     *                     property="id",
76
     *                     type="string"
77
     *                 ),
78
     *                 @OA\Property(
79
     *                     property="name",
80
     *                     type="string"
81
     *                 ),
82
     *                 example={"id": 10, "name": "Jessica Smith"}
83
     *             )
84
     *         )
85
     *     ),
86
     *     @OA\Response(
87
     *         response=200,
88
     *         description="OK"
89
     *     )
90
     * )
91
     * @return Restful
92
     */
93 1
    final public function store(): Restful
94
    {
95 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
96
    }
97
98
    /**
99
     * update
100
     *
101
     * @OA\Put(
102
     *     path="/api/v1/restful",
103
     *     summary="Modify something item",
104
     *     @OA\RequestBody(
105
     *         @OA\MediaType(
106
     *             mediaType="application/json",
107
     *             @OA\Schema(
108
     *                 @OA\Property(
109
     *                     property="id",
110
     *                     type="string"
111
     *                 ),
112
     *                 @OA\Property(
113
     *                     property="name",
114
     *                     type="string"
115
     *                 ),
116
     *                 example={"id": 10, "name": "Jessica Smoke"}
117
     *             )
118
     *         )
119
     *     ),
120
     *     @OA\Response(
121
     *         response=200,
122
     *         description="OK"
123
     *     )
124
     * )
125
     * @return Restful
126
     */
127 1
    final public function update(): Restful
128
    {
129 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
130
    }
131
132
    /**
133
     * delete
134
     *
135
     * @OA\Delete(
136
     *     path="/api/v1/restful",
137
     *     summary="Delete something item",
138
     *     @OA\RequestBody(
139
     *         @OA\MediaType(
140
     *             mediaType="application/json",
141
     *             @OA\Schema(
142
     *                 @OA\Property(
143
     *                     property="id",
144
     *                     type="string"
145
     *                 ),
146
     *                 example={"id": 10}
147
     *             )
148
     *         )
149
     *     ),
150
     *     @OA\Response(
151
     *         response=200,
152
     *         description="OK"
153
     *     )
154
     * )
155
     * @return Restful
156
     */
157 1
    final public function delete(): Restful
158
    {
159 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
160
    }
161
162
    /**
163
     * set response with for a classname and action
164
     *
165
     * @param string $classname
166
     * @param string $action
167
     * @return Restful
168
     */
169 1
    protected function setResponse(string $classname, string $action): Restful
170
    {
171 1
        $this->response
172 1
            ->setCode(Response::HTTP_OK)
173 1
            ->setContent(
174
                [
175 1
                    'error' => false,
176
                    'datas' => [
177 1
                        'method' => $this->request->getMethod(),
178 1
                        'params' => $this->request->getParams(),
179 1
                        'controller' => $classname,
180 1
                        'action' => $action,
181
                    ]
182
                ]
183
            );
184 1
        return $this;
185
    }
186
}
187