Passed
Push — master ( 6d87bc...8efc7a )
by Bence
02:13
created

Container::BuildUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CSFCloud\Containers;
4
5
use CSFCloud\KeyManager;
6
use CSFCloud\Resource;
7
use Httpful\Request;
8
9
class Container extends Resource {
10
11
    private $containerId;
12
    private $statusCache;
13
14 2
    public function __construct (KeyManager $km, string $id) {
15 2
        parent::__construct($km);
16 2
        $this->containerId = $id;
17 2
        $this->FetchStatus();
18 2
    }
19
20 2
    private function BuildUrl (string $api, array $query = []) : string {
21 2
        $query["key"] = $this->keymanager->GetServerKey();
22 2
        $url = "https://api.csfcloud.com/container/" . urlencode($this->containerId) . $api . "?" . http_build_query($query);
23
        //echo "URL: " . $url . PHP_EOL;
24 2
        return $url;
25
    }
26
27 2
    public function FetchStatus () {
28 2
        $request = Request::get($this->BuildUrl(""))->expectsText()->send();
29 2
        $this->statusCache = json_decode($request->body, true);
30 2
    }
31
32 2
    public function GetId () : string {
33 2
        return $this->containerId;
34
    }
35
36
    public function GetConfiguration () : array {
37
        return $this->statusCache["configuration"];
38
    }
39
40
    public function SetConfiguration (array $cnf) {
41
        $this->statusCache["configuration"] = $cnf;
42
    }
43
44 2
    public function UpdateChanges () {
45 2
        Request::put($this->BuildUrl(""))
46 2
            ->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send();
47 2
    }
48
49 2
    public function GetContainerName () : string {
50 2
        return $this->statusCache["configuration"]["name"];
51
    }
52
53 2
    public function SetContainerName (string $newname) {
54 2
        $this->statusCache["configuration"]["name"] = $newname;
55 2
    }
56
57
    public function IsRunning () : bool {
58
        return $this->statusCache["running"];
59
    }
60
61
    public function GetLastLogId () : ?string {
62
        return $this->statusCache["last_log"];
63
    }
64
65
    public function GetLastLogWebSocketUrl () : string {
66
        return "wss://dashboard-logs.csfcloud.com/log?id=" . $this->statusCache["last_log"];
67
    }
68
69
    public function Build () : string {
70
        $response = Request::get($this->BuildUrl("/build"))->expectsJson()->send();
71
        $this->statusCache["last_log"] = $response->body->run_id;
72
        return $this->statusCache["last_log"];
73
    }
74
75
    public function Run () : string {
76
        $response = Request::get($this->BuildUrl("/run"))->expectsJson()->send();
77
        $this->statusCache["last_log"] = $response->body->run_id;
78
        return $this->statusCache["last_log"];
79
    }
80
81
    public function Stop () {
82
        $response = Request::get($this->BuildUrl("/stop"))->expectsJson()->send();
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
83
        $this->statusCache["last_log"] = null;
84
    }
85
86 2
    public function Delete () {
87 2
        Request::delete($this->BuildUrl(""))->send();
88 2
    }
89
90 2
    private function GetFileUrl(string $name) : string {
91 2
        return $this->BuildUrl("/files", [
92 2
            "filename" => $name
93
        ]);
94
    }
95
96 2
    public function UploadFile (string $file, string $name) {
97 2
        Request::post($this->GetFileUrl($name))->body(file_get_contents($file))->send();
98 2
    }
99
100
    public function DownloadFile (string $name, string $file) {
101
        shell_exec("wget " . escapeshellarg($this->GetFileUrl($name)) . " -O " . escapeshellarg($file));
102
    }
103
104 2
    public function GetFileContents (string $name) : string {
105 2
        $response = Request::get($this->GetFileUrl($name))->send();
106 2
        return $response->body;
107
    }
108
109
    public function DeleteFile (string $name) {
110
        $url = $this->BuildUrl("/files", [
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
111
            "filename" => $name
112
        ]);
113
114
        Request::delete($this->BuildUrl(""))->send();
115
    }
116
117
}