Passed
Push — master ( 995f66...61d5bc )
by Arman
03:05 queued 12s
created

OpenApiUiAssetsCommand::exec()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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