Passed
Branch dev (32180c)
by John
03:42
created

InstallerWorker::commitCompiler()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 44
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 34
c 1
b 0
f 0
nc 20
nop 2
dl 0
loc 44
rs 8.0555
1
<?php
2
3
namespace App\Babel\Install;
4
5
use App\Models\OJModel;
6
use App\Models\CompilerModel;
7
use Exception;
8
use Throwable;
9
10
class InstallerWorker
11
{
12
    protected $ocode=null;
13
    protected $babelConfig=[];
14
    protected $oid=null;
15
    protected $command=null;
16
17
    public function __construct($ocode, $babelConfig, $oid, $command)
18
    {
19
        $this->ocode=$ocode;
20
        $this->babelConfig=$babelConfig;
21
        $this->oid=$oid;
22
        $this->command=$command;
23
    }
24
25
    public function importCSS()
26
    {
27
        $ocode=$this->ocode;
28
29
        try {
30
            if (isset($this->babelConfig["custom"]["css"]) && !is_null($this->babelConfig["custom"]["css"]) && trim($this->babelConfig["custom"]["css"])!="") {
31
                $cssPath=babel_path("Extension/$ocode/".$this->babelConfig["custom"]["css"]);
32
            } else {
33
                $cssPath=null;
34
            }
35
            $this->applyCustom($ocode, $cssPath);
36
        } catch (Throwable $e) {
37
            throw new Exception('Unable to add an custom css for this extension, aborting.');
38
        }
39
    }
40
41
    protected function applyCustom($ocode, $cssPath)
42
    {
43
        $storePath=base_path("public/static/css/oj/");
44
        if (!is_dir($storePath)) {
45
            mkdir($storePath);
46
        }
47
        if (is_null($cssPath)) {
48
            file_put_contents($storePath."$ocode.css", "\/*Silence is Golden*\/");
49
        } else {
50
            file_put_contents($storePath."$ocode.css", file_get_contents($cssPath));
51
        }
52
    }
53
54
    public function importICON()
55
    {
56
        $ocode=$this->ocode;
57
58
        try {
59
            $imgPath=babel_path("Extension/$ocode/".$this->babelConfig["icon"]);
60
            $storePath=$this->applyIcon($ocode, $imgPath);
61
            OJModel::updateOJ($this->oid, ["logo"=>$storePath]);
62
        } catch (Throwable $e) {
63
            throw new Exception('Unable to add an icon for this extension, aborting.');
64
        }
65
    }
66
67
    protected function applyIcon($ocode, $imgPath)
68
    {
69
        $storePath=base_path("public/static/img/oj/$ocode/");
70
        if (is_dir($storePath)) {
71
            delFile($storePath);
72
        } else {
73
            mkdir($storePath);
74
        }
75
        file_put_contents($storePath.basename($imgPath), file_get_contents($imgPath));
76
        return "/static/img/oj/$ocode/".basename($imgPath);
77
    }
78
79
    public function importCompilerInfo($info)
80
    {
81
        $ocode=$this->ocode;
82
        $installed_timestamp=0;
83
        if (isset($info["compiler_timestamp"]) && !is_null($info["compiler_timestamp"]) && trim($info["compiler_timestamp"])!="") {
84
            $installed_timestamp=intval($info["compiler_timestamp"]);
85
        }
86
        $latest_timestamp=$installed_timestamp;
87
        $ConpilerConfig = glob(babel_path("Extension/$ocode/compiler/*.*"));
88
        foreach ($ConpilerConfig as $file) {
89
            if (intval(basename($file)) > $installed_timestamp) {
90
                try {
91
                    $this->commitCompiler($file, json_decode(file_get_contents($file), true));
92
                    $latest_timestamp=intval(basename($file));
93
                } catch (Throwable $e) {
94
                    $this->command->line("<fg=red>Error:     ".$e->getMessage()."</>");
95
                    $this->command->line("\n  <bg=red;fg=white> Compiler info import failure, aborting. </>\n");
96
                    throw new Exception();
97
                }
98
            }
99
        }
100
        return $latest_timestamp;
101
    }
102
103
    protected function commitCompiler($path, $json)
104
    {
105
        $this->command->line("<fg=yellow>Migrating: </>$path");
106
        $modifications=$json["modifications"];
107
        foreach ($modifications as $m) {
108
            if ($m["method"]=="add") {
109
                CompilerModel::add([
110
                    "oid"=>$this->oid,
111
                    "comp"=>$m["compability"],
112
                    "lang"=>$m["language"],
113
                    "lcode"=>$m["code"],
114
                    "icon"=>$m["icon"],
115
                    "display_name"=>$m["display"],
116
                    "available"=>1,
117
                    "deleted"=>0
118
                ]);
119
            } elseif ($m["method"]=="modify") {
120
                $modifyItem=[];
121
                if (isset($m["compability"])) {
122
                    $modifyItem["comp"]=$m["compability"];
123
                }
124
                if (isset($m["language"])) {
125
                    $modifyItem["lang"]=$m["language"];
126
                }
127
                if (isset($m["icon"])) {
128
                    $modifyItem["icon"]=$m["icon"];
129
                }
130
                if (isset($m["display"])) {
131
                    $modifyItem["display_name"]=$m["display"];
132
                }
133
                CompilerModel::modify([
134
                    "oid"=>$this->oid,
135
                    "lcode"=>$m["code"],
136
                ], $modifyItem);
137
            } elseif ($m["method"]=="remove") {
138
                CompilerModel::remove([
139
                    "oid"=>$this->oid,
140
                    "lcode"=>$m["code"],
141
                ]);
142
            } else {
143
                continue;
144
            }
145
        }
146
        $this->command->line("<fg=green>Migrated:  </>$path");
147
    }
148
}
149