Passed
Pull Request — master (#87)
by
unknown
02:48
created

GenerateSwaggerUiDocsCommand::exec()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 56
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 27
nc 14
nop 0
dl 0
loc 56
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Console\Commands;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Console\QtCommand;
19
20
/**
21
 * Class GenerateSwaggerUiDocsCommand
22
 * @package Quantum\Console\Commands
23
 */
24
class GenerateSwaggerUiDocsCommand extends QtCommand
25
{
26
    /**
27
     * Command name
28
     * @var string
29
     */
30
    protected $name = 'install:swagger';
31
32
    /**
33
     * Command description
34
     * @var string
35
     */
36
    protected $description = 'Generates files for swagger ui';
37
38
    /**
39
     * Command arguments
40
     * @var \string[][]
41
     */
42
    protected $args = [
43
        ['module', 'required', 'The module name'],
44
    ];
45
46
    /**
47
     * Command help text
48
     * @var string
49
     */
50
    protected $help = 'The command will publish swagger ui resources';
51
52
    /**
53
     * Path to public debug bar resources
54
     * @var string 
55
     */
56
    private $publicSwaggerFolderPath = 'public/assets/SwaggerUi';
57
58
    /**
59
     * Path to vendor debug bar resources
60
     * @var string 
61
     */
62
    private $vendorSwaggerFolderPath = 'vendor/swagger-api/swagger-ui/dist';
63
64
    /**
65
     * Exclude File Names
66
     * @var array
67
     */
68
    private $excludeFileNames = ['index.html', 'swagger-initializer.js', 'favicon-16x16.png', 'favicon-32x32.png'];
69
70
    /**
71
     * Executes the command and publishes the debug bar assets
72
     */
73
    public function exec()
74
    {
75
        $fs = new FileSystem();
76
        $module = $this->getArgument('module');
77
        $modulePath = modules_dir() . DS . $module;
78
        $file = $modulePath . DS . 'Config' . DS . 'routes.php';
79
        $openApiRoutes = "
80
use Quantum\Libraries\Storage\FileSystem;
81
use Quantum\Http\Response;
82
use Quantum\Di\Di;
83
84
return function (\$route) {
85
    //\$route->group('openapi', function (\$route) {
86
        \$route->get('" . strtolower($module) . "/documentation', function (Response \$response) {
87
            \$response->html(partial('swagger/swagger'));
88
        });
89
90
        \$route->get('" . strtolower($module) . "/docs', function (Response \$response) {
91
            \$fs = Di::get(FileSystem::class);
92
            \$response->json((array) json_decode(\$fs->get(modules_dir() . DS . '" . $module . "' . DS . 'Resources' . DS . 'swagger' . DS . 'docs.json', true)));
93
        //});
94
    });";
95
        if (strpos($fs->get($file), "\$route->group('openapi', function (\$route) {") === false) {
96
            $fs->put($file, str_replace('return function ($route) {', $openApiRoutes, $fs->get($file)));
97
        }
98
        if (!is_dir($modulePath)) {
99
            $this->error('The module ' . $module . ' not found');
100
            return;
101
        }
102
103
        if (!installed($modulePath . DS . 'swagger' . DS . 'docs.json')) {
104
            $fp = fopen($modulePath . DS . 'Resources' . DS . "swagger" . DS . "docs.json", "wb");
105
            fclose($fp);
106
        }
107
108
        exec(base_dir() . DS . 'vendor/bin/openapi modules/' . $module . '/Controllers/ -o modules/' . $module . '/Resources/swagger/docs.json');
109
110
111
        if (installed(assets_dir() . DS . 'SwaggerUi' . DS . 'index.css')) {
112
            $this->error('The swagger ui already installed');
113
            return;
114
        }
115
116
        $dir = opendir($this->vendorSwaggerFolderPath);
117
118
        if (is_resource($dir)) {
119
            while (($file = readdir($dir))) {
120
                if ($file && ($file != '.') && ($file != '..') && !in_array($file, $this->excludeFileNames)) {
121
                    copy($this->vendorSwaggerFolderPath . '/' . $file, $this->publicSwaggerFolderPath . '/' . $file);
122
                }
123
            }
124
125
            closedir($dir);
126
        }
127
128
        $this->info('Swagger assets successfully published');
129
    }
130
}
131