Passed
Pull Request — master (#182)
by John
04:01
created

Install::handle()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
cc 7
eloc 21
c 3
b 0
f 3
nc 5
nop 0
dl 0
loc 26
rs 8.6506
1
<?php
2
3
namespace App\Console\Commands\Babel;
4
5
use Illuminate\Console\Command;
6
use Exception;
7
use function GuzzleHttp\json_decode;
8
use Symfony\Component\Console\Output\BufferedOutput;
9
10
class Install extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'babel:install {extension : The package name of the extension} {--ignore-platform-reqs : Ignore the Platform Requirements when install}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Install a given Babel Extension to NOJ';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $extension = $this->argument('extension');
44
        $ignoreReqs = $this->option('ignore-platform-reqs');
0 ignored issues
show
Unused Code introduced by
The assignment to $ignoreReqs is dead and can be removed.
Loading history...
45
        $output = new BufferedOutput();
46
        $installerProvider="Installer";
0 ignored issues
show
Unused Code introduced by
The assignment to $installerProvider is dead and can be removed.
Loading history...
47
        try {
48
            $BabelConfig=json_decode(file_get_contents(babel_path("Extension/$extension/babel.json")), true);
49
        }catch(Exception $e){
50
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>babel.json parse error, The extension may not exist.</>\n");
51
            if($this->confirm("Would you like to download it from the marketspace first?")){
52
                $this->call("babel:require", ['extension' => $extension]);
53
                $output->fetch();
54
            }
55
            return;
56
        }
57
        if(!isset($BabelConfig["provider"]["installer"]) || trim($BabelConfig["provider"]["installer"])=="" || is_null($BabelConfig["provider"]["installer"])){
58
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>Installer not provided.</>\n");
59
            return;
60
        }
61
        $installerProvider=$BabelConfig["provider"]["installer"];
62
        $installer=self::create($extension,$installerProvider,$this);
63
        if(!is_null($installer)) {
64
            $installer->install();
65
        } else {
66
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>Installer initiation error.</>\n");
67
        }
68
    }
69
70
    public static function create($oj,$installerProvider,$class) {
71
        $className = "App\\Babel\\Extension\\$oj\\$installerProvider";
72
        if(class_exists($className)) {
73
            return new $className($class);
74
        } else {
75
            return null;
76
        }
77
    }
78
}
79