Passed
Push — master ( 304251...6faf29 )
by Fran
04:08
created

DocumentorController::swaggerUi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace PSFS\controller;
3
4
use PSFS\base\Router;
5
use PSFS\base\types\Controller;
6
use PSFS\services\DocumentorService;
7
8
class DocumentorController extends Controller {
9
    const DOMAIN = 'ROOT';
10
11
    /**
12
     * @Inyectable
13
     * @var \PSFS\services\DocumentorService $srv
14
     */
15
    protected $srv;
16
17
    /**
18
     * @GET
19
     * @CACHE 600
20
     * @label Generador de documentación API
21
     * @route /{domain}/api/doc
22
     *
23
     * @param string $domain
24
     *
25
     * @return string JSON
26
     */
27
    public function createApiDocs($domain)
28
    {
29
        ini_set('memory_limit', -1);
30
        ini_set('max_execution_time', -1);
31
32
        $type = $this->getRequest()->get('type') ?: ApiController::PSFS_DOC;
33
        $download = $this->getRequest()->get('download') ?: false;
34
35
        $module = $this->srv->getModules($domain);
36
        if(empty($module)) {
37
            return Router::getInstance()->httpNotFound(null, true);
38
        }
39
        switch (strtolower($type)) {
40
            case ApiController::SWAGGER_DOC:
41
                $doc = DocumentorService::swaggerFormatter($module);
42
                break;
43
            case ApiController::POSTMAN_DOC:
44
                $doc = ['Pending...'];
45
                break;
46
            default:
47
            case ApiController::HTML_DOC:
48
            case ApiController::PSFS_DOC:
49
                $doc = $this->srv->extractApiEndpoints($module);
50
                break;
51
        }
52
53
        ini_restore('max_execution_time');
54
        ini_restore('memory_limit');
55
56
        if($download && $type === ApiController::SWAGGER_DOC) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $download of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
            return $this->download(\GuzzleHttp\json_encode($doc), 'application/json', 'swagger.json');
58
        } elseif($type === ApiController::HTML_DOC) {
59
            return $this->render('documentation.html.twig', ["data" => json_encode($doc)]);
60
        } else {
61
            return $this->json($doc, 200);
62
        }
63
    }
64
65
    /**
66
     * @GET
67
     * @route /admin/{domain}/swagger-ui
68
     * @param string $domain
69
     * @return string HTML
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
70
     */
71
    public function swaggerUi($domain) {
72
        return $this->render('swagger.html.twig', [
73
            'domain' => $domain,
74
        ]);
75
    }
76
}