Passed
Push — master ( 821d9b...d02e66 )
by Arthur
06:56 queued 03:25
created

Documentation::setLicenseInfo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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