Passed
Pull Request — master (#235)
by John
05:34
created

ExtensionModel::getRemote()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Models\Babel;
4
5
use Illuminate\Database\Eloquent\Model;
6
use App\Models\Eloquent\OJModel;
7
use PharIo\Version\Version;
8
use Throwable;
9
10
class ExtensionModel extends Model
11
{
12
13
    public static $status=[
14
        "-1"=>"Extension Fatal Error",
15
        "0"=>"Not Downloaded",
16
        "1"=>"Not Installed",
17
        "2"=>"Installed",
18
    ];
19
20
    public static function list()
21
    {
22
        $ret=[];
23
        $marketspaceRaw=self::getRemote();
24
        if(empty($marketspaceRaw)) return [];
25
        foreach($marketspaceRaw["packages"] as $extension){
26
            $temp=[
27
                "details"=>$extension,
28
                "status"=>0,
29
                "version"=>null,
30
                "updatable"=>false,
31
                "settings"=>null,
32
                "available"=>null
33
            ];
34
            $temp["details"]["typeParsed"]=$temp["details"]["type"]=="virtual-judge"?"VirtualJudge":"OnlineJudge";
35
            try {
36
                try {
37
                    $BabelConfig=json_decode(file_get_contents(babel_path("Extension/{$extension['code']}/babel.json")), true);
38
                }catch (Throwable $e){
39
                    $BabelConfig=[];
40
                }
41
                if (!empty($BabelConfig)) {
42
                    if ($BabelConfig["version"]=='__cur__') {
43
                        $BabelConfig["version"]=explode("-", version())[0];
44
                    }
45
                    $downloadedVersion=new Version($BabelConfig["version"]);
46
                    $remoteVersion=new Version($extension["version"]);
47
                    $temp["updatable"]=$remoteVersion->isGreaterThan($downloadedVersion);
48
49
                    $installedConfig=OJModel::where(["ocode"=>$extension["code"]])->first();
50
                    if (is_null($installedConfig)){
51
                        $temp["status"]=1;
52
                    } else {
53
                        $temp["version"]=$installedConfig->version; // local installed version
54
                        $installedVersion=new Version($temp["version"]);
55
                        if ($downloadedVersion->isGreaterThan($installedVersion)){
56
                            $temp["status"]=1;
57
                        } else {
58
                            $temp["status"]=2;
59
                        }
60
                        $temp["settings"]=false;
61
                        $temp["available"]=$installedConfig->status;
62
                    }
63
                }
64
            }catch (Throwable $e){
65
                $temp["status"]=-1;
66
            }
67
            $ret[]=$temp;
68
        }
69
70
        return $ret;
71
    }
72
73
    public static function getRemote()
74
    {
75
        try {
76
            return json_decode(file_get_contents(env("BABEL_MIRROR", "https://acm.njupt.edu.cn/babel")."/babel.json"), true);
77
        }catch(Throwable $e){
78
            return [];
79
        }
80
    }
81
}
82