Passed
Pull Request — master (#87)
by
unknown
13:19 queued 10:11
created

OpenApiUiAssetsCommand::exec()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   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
    /**
28
     * File System
29
     * @var \Quantum\Libraries\Storage\FileSystem
30
     */
31
    protected $fs;
32
33
    /**
34
     * Command name
35
     * @var string
36
     */
37
    protected $name = 'install:openapi';
38
39
    /**
40
     * Command description
41
     * @var string
42
     */
43
    protected $description = 'Generates files for OpenApi UI';
44
45
    /**
46
     * Command arguments
47
     * @var \string[][]
48
     */
49
    protected $args = [
50
        ['module', 'required', 'The module name'],
51
    ];
52
53
    /**
54
     * Command help text
55
     * @var string
56
     */
57
    protected $help = 'The command will publish OpenApi UI resources';
58
59
    /**
60
     * Path to public debug bar resources
61
     * @var string 
62
     */
63
    private $publicOpenApiFolderPath = 'public/assets/OpenApiUi';
64
65
    /**
66
     * Path to vendor debug bar resources
67
     * @var string 
68
     */
69
    private $vendorOpenApiFolderPath = 'vendor/swagger-api/swagger-ui/dist';
70
71
    /**
72
     * Exclude File Names
73
     * @var array
74
     */
75
    private $excludeFileNames = ['index.html', 'swagger-initializer.js', 'favicon-16x16.png', 'favicon-32x32.png'];
76
77
    /**
78
     * Executes the command and publishes the debug bar assets
79
     */
80
    public function exec()
81
    {
82
        $this->fs = Di::get(FileSystem::class);
83
        $module = $this->getArgument('module');
84
        $modulePath = modules_dir() . DS . $module;
85
        $file = $modulePath . DS . 'Config' . DS . 'routes.php';
86
        $openapiRoutes = 'return function ($route) {
87
    //$route->group("openapi", function ($route) {
88
        $route->get("' . strtolower($module) . '/documentation", function (Quantum\Http\Response $response) {
89
            $response->html(partial("openapi/openapi"));
90
        });
91
92
        $route->get("' . strtolower($module) . '/docs", function (Quantum\Http\Response $response) {
93
            $fs = Quantum\Di\Di::get(Quantum\Libraries\Storage\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 . DS . $fileUi, $this->publicOpenApiFolderPath . DS . $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 . 'Resources' . DS . 'openApi' . DS . 'docs.json')) {
122
            $this->fs->put($modulePath . DS . 'Resources' . DS . 'openApi' . DS . 'docs.json');
123
        }
124
125
        exec(base_dir() . DS . 'vendor' . DS . 'bin' . DS . 'openapi modules' . DS . $module . DS . 'Controllers' . DS . ' -o modules' . DS . $module .  DS . 'Resources' . DS . 'openApi' . DS . 'docs.json');
126
127
128
        $this->info('OpenApi assets successfully published');
129
    }
130
}
131