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

Install::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 6
rs 10
c 1
b 0
f 1
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}';
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
        $output = new BufferedOutput();
45
        $installerProvider="Installer";
0 ignored issues
show
Unused Code introduced by
The assignment to $installerProvider is dead and can be removed.
Loading history...
46
        try {
47
            $BabelConfig=json_decode(file_get_contents(babel_path("Extension/$extension/babel.json")), true);
48
        }catch(Exception $e){
49
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>babel.json parse error, The extension may not exist.</>\n");
50
            if($this->confirm("Would you like to download it from the marketspace first?")){
51
                $this->call("babel:require", ['extension' => $extension]);
52
                $output->fetch();
53
            }
54
            return;
55
        }
56
        if(!isset($BabelConfig["provider"]["installer"]) || trim($BabelConfig["provider"]["installer"])=="" || is_null($BabelConfig["provider"]["installer"])){
57
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>Installer not provided.</>\n");
58
            return;
59
        }
60
        $installerProvider=$BabelConfig["provider"]["installer"];
61
        $installer=self::create($extension,$installerProvider,$this);
62
        if(!is_null($installer)) {
63
            $installer->install();
64
        } else {
65
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>Installer initiation error.</>\n");
66
        }
67
    }
68
69
    public static function create($oj,$installerProvider,$class) {
70
        $className = "App\\Babel\\Extension\\$oj\\$installerProvider";
71
        if(class_exists($className)) {
72
            return new $className($class);
73
        } else {
74
            return null;
75
        }
76
    }
77
}
78