Passed
Push — master ( 67d050...43d777 )
by Arthur
03:12
created

Documentation::setUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\ApiGenerator;
6
use SoliDry\Controllers\BaseCommand;
7
use SoliDry\Helpers\Classes;
8
use SoliDry\Types\ApiInterface;
9
use SoliDry\Types\DefaultInterface;
10
use SoliDry\Types\DocumentationInterface;
11
use SoliDry\Types\PhpInterface;
12
13
/**
14
 * Class Documentation
15
 *
16
 * @package SoliDry\Blocks
17
 *
18
 * @property BaseCommand generator
19
 */
20
abstract class Documentation
21
{
22
23
    use ContentManager;
0 ignored issues
show
introduced by
The trait SoliDry\Blocks\ContentManager requires some properties which are not provided by SoliDry\Blocks\Documentation: $options, $version, $modulesDir
Loading history...
24
25
    protected $generator;
26
    protected $sourceCode = '';
27
    protected $className;
28
29
    /**
30
     * Controllers constructor.
31
     *
32
     * @param ApiGenerator $generator
33
     */
34
    public function __construct($generator)
35
    {
36
        $this->generator = $generator;
37
        $this->className = Classes::getClassName($this->generator->objectName);
38
    }
39
40
    protected function setDefaultDocs()
41
    {
42
        $this->setComment(DefaultInterface::METHOD_START);
43
44
        $this->openComment();
45
46
        // generate basic info
47
        $this->setStarredComment(DocumentationInterface::OA_INFO . PhpInterface::OPEN_PARENTHESES);
48
49
        $this->setInfoParams();
50
51
        // generate contact info
52
        $this->setContactInfo();
53
54
        // generate license info
55
        $this->setLicenseInfo();
56
57
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES);
58
59
        $this->closeComment();
60
61
        $this->setComment(DefaultInterface::METHOD_END);
62
    }
63
64
    /**
65
     *  Sets info params - title, version, description
66
     */
67
    private function setInfoParams(): void
68
    {
69
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_TITLE]) === false) {
70
            $this->setStarredComment('title="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_TITLE] . '",',
71
                1, 1);
72
        }
73
74
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_VERSION]) === false) {
75
            $this->setStarredComment('version="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_VERSION] . '",',
76
                1, 1);
77
        }
78
79
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_DESCRIPTION]) === false) {
80
            $this->setStarredComment('description="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_DESCRIPTION] . '",',
81
                1, 1);
82
        }
83
    }
84
85
    /**
86
     *  Sets license info - name, url
87
     */
88
    private function setLicenseInfo(): void
89
    {
90
        $this->setStarredComment(DocumentationInterface::OA_LICENSE . PhpInterface::OPEN_PARENTHESES,
91
            1, 1);
92
93
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_NAME]) === false) {
94
            $this->setStarredComment('name="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_NAME] . '",',
95
                1, 2);
96
        }
97
98
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_URL]) === false) {
99
            $this->setStarredComment('url="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_LICENSE][ApiInterface::API_URL] . '",',
100
                1, 2);
101
        }
102
103
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1);
104
    }
105
106
    /**
107
     *  Sets contact info - email, name, url
108
     */
109
    private function setContactInfo(): void
110
    {
111
        $this->setStarredComment(DocumentationInterface::OA_CONTACT . PhpInterface::OPEN_PARENTHESES,
112
            1, 1);
113
114
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_EMAIL]) === false) {
115
            $this->setStarredComment('email="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_EMAIL] . '",',
116
                1, 2);
117
        }
118
119
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_NAME]) === false) {
120
            $this->setStarredComment('name="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_NAME] . '",',
121
                1, 2);
122
        }
123
124
        if (empty($this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_URL]) === false) {
125
            $this->setStarredComment('url="' . $this->generator->data[ApiInterface::API_INFO][ApiInterface::API_CONTACT][ApiInterface::API_URL] . '",',
126
                1, 2);
127
        }
128
129
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1);
130
    }
131
132
    /**
133
     *  Sets doc comments for every controller
134
     */
135
    protected function setControllersDocs(): void
136
    {
137
        $this->setComment(DefaultInterface::METHOD_START);
138
139
        $this->setIndex();
140
141
        $this->setView();
142
143
        $this->setCreate();
144
145
        $this->setUpdate();
146
147
        $this->setDelete();
148
149
        $this->setComment(DefaultInterface::METHOD_END);
150
    }
151
152
    private function setIndex(): void
153
    {
154
        $this->openComment();
155
156
        $this->setStarredComment(DocumentationInterface::OA_GET . PhpInterface::OPEN_PARENTHESES);
157
158
        $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH
159
            . strtolower($this->generator->objectName) . '",', 1, 1);
160
161
        $this->setStarredComment('summary="Get ' . $this->generator->objectName . 's ",', 1, 1);
162
163
        $this->setStarredComment('tags={"' . $this->generator->objectName . DefaultInterface::CONTROLLER_POSTFIX
164
            . '"},', 1, 1);
165
166
        // define params
167
        $this->setParameter([
168
            'in'       => '"query"',
169
            'name'     => '"include"',
170
            'required' => 'false',
171
        ]);
172
173
        $this->setParameter([
174
            'in'       => '"query"',
175
            'name'     => '"page"',
176
            'required' => 'false',
177
        ], 'integer');
178
179
        $this->setParameter([
180
            'in'       => '"query"',
181
            'name'     => '"limit"',
182
            'required' => 'false',
183
        ]);
184
185
        $this->setParameter([
186
            'in'       => '"query"',
187
            'name'     => '"sort"',
188
            'required' => 'false',
189
        ]);
190
191
        $this->setParameter([
192
            'in'       => '"query"',
193
            'name'     => '"data"',
194
            'required' => 'false',
195
        ]);
196
197
        $this->setParameter([
198
            'in'       => '"query"',
199
            'name'     => '"filter"',
200
            'required' => 'false',
201
        ]);
202
203
        $this->setParameter([
204
            'in'       => '"query"',
205
            'name'     => '"order_by"',
206
            'required' => 'false',
207
        ]);
208
209
        $this->setResponse([
210
            'response'    => '200',
211
            'description' => '""',
212
        ]);
213
214
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES);
215
216
        $this->closeComment();
217
        $this->setNewLines();
218
    }
219
220
    private function setView(): void
221
    {
222
        $this->openComment();
223
224
        $this->setStarredComment(DocumentationInterface::OA_GET . PhpInterface::OPEN_PARENTHESES);
225
226
        $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH
227
            . strtolower($this->generator->objectName) . PhpInterface::SLASH . '{id}",', 1, 1);
228
229
        $this->setStarredComment('summary="Get ' . $this->generator->objectName . '",', 1, 1);
230
231
        $this->setStarredComment('tags={"' . $this->generator->objectName . DefaultInterface::CONTROLLER_POSTFIX
232
            . '"},', 1, 1);
233
234
        $this->setParameter([
235
            'in'       => '"query"',
236
            'name'     => '"include"',
237
            'required' => 'false',
238
        ]);
239
240
        $this->setParameter([
241
            'in'       => '"query"',
242
            'name'     => '"data"',
243
            'required' => 'false',
244
        ]);
245
246
        $this->setResponse([
247
            'response'    => '200',
248
            'description' => '""',
249
        ]);
250
251
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES);
252
253
        $this->closeComment();
254
        $this->setNewLines();
255
    }
256
257
    private function setCreate(): void
258
    {
259
        $this->openComment();
260
261
        $this->setStarredComment(DocumentationInterface::OA_POST . PhpInterface::OPEN_PARENTHESES);
262
263
        $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH
264
            . strtolower($this->generator->objectName) . '",', 1, 1);
265
266
        $this->setStarredComment('summary="Create ' . $this->generator->objectName . '",', 1, 1);
267
268
        $this->setStarredComment('tags={"' . $this->generator->objectName . DefaultInterface::CONTROLLER_POSTFIX
269
            . '"},', 1, 1);
270
271
        $this->setResponse([
272
            'response'    => '200',
273
            'description' => '""',
274
        ]);
275
276
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES);
277
278
        $this->closeComment();
279
        $this->setNewLines();
280
    }
281
282
    private function setUpdate(): void
283
    {
284
        $this->openComment();
285
286
        $this->setStarredComment(DocumentationInterface::OA_PATCH . PhpInterface::OPEN_PARENTHESES);
287
288
        $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH
289
            . strtolower($this->generator->objectName) . PhpInterface::SLASH . '{id}",', 1, 1);
290
291
        $this->setStarredComment('summary="Update ' . $this->generator->objectName . '",', 1, 1);
292
293
        $this->setStarredComment('tags={"' . $this->generator->objectName . DefaultInterface::CONTROLLER_POSTFIX
294
            . '"},', 1, 1);
295
296
        $this->setResponse([
297
            'response'    => '200',
298
            'description' => '""',
299
        ]);
300
301
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES);
302
303
        $this->closeComment();
304
        $this->setNewLines();
305
    }
306
307
    private function setDelete(): void
308
    {
309
        $this->openComment();
310
311
        $this->setStarredComment(DocumentationInterface::OA_DELETE . PhpInterface::OPEN_PARENTHESES);
312
313
        $this->setStarredComment('path="' . PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH
314
            . strtolower($this->generator->objectName) . PhpInterface::SLASH . '{id}",', 1, 1);
315
316
        $this->setStarredComment('summary="Delete ' . $this->generator->objectName . '",', 1, 1);
317
318
        $this->setStarredComment('tags={"' . $this->generator->objectName . DefaultInterface::CONTROLLER_POSTFIX
319
            . '"},', 1, 1);
320
321
        $this->setResponse([
322
            'response'    => '200',
323
            'description' => '""',
324
        ]);
325
326
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES);
327
328
        $this->closeComment();
329
        $this->setNewLines();
330
    }
331
332
    /**
333
     * Sets any params of methods
334
     *
335
     * @param array $paramValues
336
     * @param string $schemaType
337
     */
338
    private function setParameter(array $paramValues, string $schemaType = 'string'): void
339
    {
340
        $this->setStarredComment(DocumentationInterface::OA_PARAMETER . PhpInterface::OPEN_PARENTHESES, 1, 1);
341
        foreach ($paramValues as $key => $val) {
342
            $this->setStarredComment($key . '=' . $val . ',', 1, 2);
343
        }
344
345
        $this->setSchema($schemaType);
346
347
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1);
348
    }
349
350
    /**
351
     * Sets the parameter schema
352
     *
353
     * @param string $schemaType
354
     */
355
    private function setSchema(string $schemaType): void
356
    {
357
        $this->setStarredComment(DocumentationInterface::OA_SCHEMA . PhpInterface::OPEN_PARENTHESES, 1, 2);
358
359
        $this->setStarredComment('type="' . $schemaType . '",', 1, 3);
360
361
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 2);
362
    }
363
364
    /**
365
     * Sets any response of method
366
     *
367
     * @param array $paramValues
368
     */
369
    private function setResponse(array $paramValues): void
370
    {
371
        $this->setStarredComment(DocumentationInterface::OA_RESPONSE . PhpInterface::OPEN_PARENTHESES, 1, 1);
372
        foreach ($paramValues as $key => $val) {
373
            $this->setStarredComment($key . '=' . $val . ',', 1, 2);
374
        }
375
376
        $this->setStarredComment(PhpInterface::CLOSE_PARENTHESES . PhpInterface::COMMA, 1, 1);
377
    }
378
}