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

OpenApiUiAssetsCommand::exec()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 55
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
c 0
b 0
f 0
nc 13
nop 0
dl 0
loc 55
rs 7.3166

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
use Quantum\Di\Di;
20
21
/**
22
 * Class OpenApiUiAssetsCommand
23
 * @package Quantum\Console\Commands
24
 */
25
class OpenApiUiAssetsCommand extends QtCommand
26
{
27
    protected $fs;
28
    /**
29
     * Command name
30
     * @var string
31
     */
32
    protected $name = 'install:openapi';
33
34
    /**
35
     * Command description
36
     * @var string
37
     */
38
    protected $description = 'Generates files for OpenApi UI';
39
40
    /**
41
     * Command arguments
42
     * @var \string[][]
43
     */
44
    protected $args = [
45
        ['module', 'required', 'The module name'],
46
    ];
47
48
    /**
49
     * Command help text
50
     * @var string
51
     */
52
    protected $help = 'The command will publish OpenApi UI resources';
53
54
    /**
55
     * Path to public debug bar resources
56
     * @var string 
57
     */
58
    private $publicOpenApiFolderPath = 'public/assets/OpenApiUi';
59
60
    /**
61
     * Path to vendor debug bar resources
62
     * @var string 
63
     */
64
    private $vendorOpenApiFolderPath = 'vendor/swagger-api/swagger-ui/dist';
65
66
    /**
67
     * Exclude File Names
68
     * @var array
69
     */
70
    private $excludeFileNames = ['index.html', 'swagger-initializer.js', 'favicon-16x16.png', 'favicon-32x32.png'];
71
72
    /**
73
     * Executes the command and publishes the debug bar assets
74
     */
75
    public function exec()
76
    {
77
        $this->fs = Di::get(FileSystem::class);
78
        $module = $this->getArgument('module');
79
        $modulePath = modules_dir() . DS . $module;
80
        $file = $modulePath . DS . 'Config' . DS . 'routes.php';
81
        $openapiRoutes = "
82
use Quantum\Libraries\Storage\FileSystem;
83
use Quantum\Http\Response;
84
use Quantum\Di\Di;
85
86
return function (\$route) {
87
    //\$route->group('openapi', function (\$route) {
88
        \$route->get('" . strtolower($module) . "/documentation', function (Response \$response) {
89
            \$response->html(partial('openapi/openapi'));
90
        });
91
92
        \$route->get('" . strtolower($module) . "/docs', function (Response \$response) {
93
            \$fs = Di::get(FileSystem::class);
94
            \$response->json((array) json_decode(\$fs->get(modules_dir() . DS . '" . $module . "' . DS . 'Resources' . DS . 'openapi' . DS . 'docs.json', true)));
95
        //});
96
    });";
97
98
        if (!$this->fs->isDirectory($modulePath)) {
99
            $this->error('The module ' . $module . ' not found');
100
            return;
101
        }
102
103
        if (!$this->fs->exists(assets_dir() . DS . 'OpenApiUi' . DS . 'index.css')) {
104
            $dir = opendir($this->vendorOpenApiFolderPath);
105
106
            if (is_resource($dir)) {
107
                while (($fileUi = readdir($dir))) {
108
                    if ($fileUi && ($fileUi != '.') && ($fileUi != '..') && !in_array($fileUi, $this->excludeFileNames)) {
109
                        copy($this->vendorOpenApiFolderPath . '/' . $fileUi, $this->publicOpenApiFolderPath . '/' . $fileUi);
110
                    }
111
                }
112
113
                closedir($dir);
114
            }
115
        }
116
117
        if (strpos($this->fs->get($file), "\$route->group('openapi', function (\$route) {") === false) {
118
            $this->fs->put($file, str_replace('return function ($route) {', $openapiRoutes, $this->fs->get($file)));
119
        }
120
121
        if ($this->fs->exists($modulePath . DS . 'openApi' . DS . 'docs.json')) {
122
            $fp = fopen($modulePath . DS . 'Resources' . DS . "openApi" . DS . "docs.json", "w");
123
            fclose($fp);
124
        }
125
126
        exec(base_dir() . DS . 'vendor/bin/openapi modules/' . $module . '/Controllers/ -o modules/' . $module . '/Resources/openApi/docs.json');
127
128
129
        $this->info('OpenApi assets successfully published');
130
    }
131
}
132