Completed
Pull Request — master (#973)
by
unknown
03:35
created

InstallCommand::installFromFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 12
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Json;
7
use Nwidart\Modules\Process\Installer;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class InstallCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'module:install';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Install the specified module by given package name (vendor/name).';
26
27
    /**
28
     * Execute the console command.
29
     */
30
    public function handle()
31
    {
32
        if (is_null($this->argument('name'))) {
33
            $this->installFromFile();
34
35
            return;
36
        }
37
38
        $this->install(
39
            $this->argument('name'),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array; however, Nwidart\Modules\Commands\InstallCommand::install() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
            $this->argument('version'),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('version') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array; however, Nwidart\Modules\Commands\InstallCommand::install() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
41
            $this->option('type'),
42
            $this->option('tree')
43
        );
44
    }
45
46
    /**
47
     * Install modules from modules.json file.
48
     */
49
    protected function installFromFile()
50
    {
51
        if (!file_exists($path = base_path('modules.json'))) {
52
            $this->error("File 'modules.json' does not exist in your project root.");
53
54
            return;
55
        }
56
57
        $modules = Json::make($path);
58
59
        $dependencies = $modules->get('require', []);
60
61
        foreach ($dependencies as $module) {
62
            $module = collect($module);
63
64
            $this->install(
65
                $module->get('name'),
66
                $module->get('version'),
67
                $module->get('type')
68
            );
69
        }
70
    }
71
72
    /**
73
     * Install the specified module.
74
     *
75
     * @param string $name
76
     * @param string $version
77
     * @param string $type
78
     * @param bool   $tree
79
     */
80
    protected function install($name, $version = 'dev-master', $type = 'composer', $tree = false)
81
    {
82
        $installer = new Installer(
83
            $name,
84
            $version,
85
            $type ?: $this->option('type'),
86
            $tree ?: $this->option('tree')
87
        );
88
89
        $installer->setRepository($this->laravel['modules']);
90
91
        $installer->setConsole($this);
92
93
        if ($timeout = $this->option('timeout')) {
94
            $installer->setTimeout($timeout);
95
        }
96
97
        if ($path = $this->option('path')) {
98
            $installer->setPath($path);
99
        }
100
101
        $installer->run();
102
103
        if (!$this->option('no-update')) {
104
            $this->call('module:update', [
105
                'module' => $installer->getModuleName(),
106
            ]);
107
        }
108
    }
109
110
    /**
111
     * Get the console command arguments.
112
     *
113
     * @return array
114
     */
115 127
    protected function getArguments()
116
    {
117
        return [
118 127
            ['name', InputArgument::OPTIONAL, 'The name of module will be installed.'],
119
            ['version', InputArgument::OPTIONAL, 'The version of module will be installed.'],
120
        ];
121
    }
122
123
    /**
124
     * Get the console command options.
125
     *
126
     * @return array
127
     */
128 127
    protected function getOptions()
129
    {
130
        return [
131 127
            ['timeout', null, InputOption::VALUE_OPTIONAL, 'The process timeout.', null],
132
            ['path', null, InputOption::VALUE_OPTIONAL, 'The installation path.', null],
133
            ['type', null, InputOption::VALUE_OPTIONAL, 'The type of installation.', null],
134
            ['tree', null, InputOption::VALUE_NONE, 'Install the module as a git subtree', null],
135
            ['no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies.', null],
136
        ];
137
    }
138
}
139