Test Failed
Push — master ( 3448ae...2af642 )
by Bence
03:55
created

Container::GetFileUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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
        return "https://api.csfcloud.com/container/" . urlencode($this->containerId) . "" . $api . http_build_query($query);
23 2
    }
24
25 2
    public function FetchStatus () {
26 2
        $request = Request::get($this->BuildUrl(""))->expectsText()->send();
27
        $this->statusCache = json_decode($request->body, true);
28
    }
29
30
    public function GetId () : string {
31
        return $this->containerId;
32
    }
33
34
    public function GetConfiguration () : array {
35
        return $this->statusCache["configuration"];
36
    }
37 2
38 2
    public function SetConfiguration (array $cnf) {
39 2
        $this->statusCache["configuration"] = $cnf;
40 2
    }
41
42 2
    public function UpdateChanges () {
43 2
        Request::put($this->BuildUrl(""))
44
            ->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send();
45
    }
46 2
47 2
    public function GetContainerName () : string {
48 2
        return $this->statusCache["configuration"]["name"];
49
    }
50
51
    public function SetContainerName (string $newname) {
52
        $this->statusCache["configuration"]["name"] = $newname;
53
    }
54
55
    public function IsRunning () : bool {
56
        return $this->statusCache["running"];
57
    }
58
59
    public function GetLastLogId () : ?string {
60
        return $this->statusCache["last_log"];
61
    }
62
63
    public function GetLastLogWebSocketUrl () : string {
64
        return "wss://dashboard-logs.csfcloud.com/log?id=" . $this->statusCache["last_log"];
65
    }
66
67
    public function Build () : string {
68
        $response = Request::get($this->BuildUrl("/build"))->expectsJson()->send();
69
        $this->statusCache["last_log"] = $response->body->run_id;
70
        return $this->statusCache["last_log"];
71
    }
72
73
    public function Run () : string {
74 2
        $response = Request::get($this->BuildUrl("/run"))->expectsJson()->send();
75 2
        $this->statusCache["last_log"] = $response->body->run_id;
76 2
        return $this->statusCache["last_log"];
77
    }
78
79
    public function Stop () {
80
        $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...
81
        $this->statusCache["last_log"] = null;
82
    }
83
84
    public function Delete () {
85
        Request::delete($this->BuildUrl(""))->send();
86
    }
87
88
    private function GetFileUrl(string $name) : string {
89
        return $this->BuildUrl("/files", [
90
            "filename" => $name
91
        ]);
92
    }
93
94
    public function UploadFile (string $file, string $name) {
95
        Request::post($this->GetFileUrl($name))->body(file_get_contents($file))->send();
96
    }
97
98
    public function DownloadFile (string $name, string $file) {
99
        shell_exec("wget " . escapeshellarg($this->GetFileUrl($name)) . " -O " . escapeshellarg($file));
100
    }
101
102
    public function GetFileContents (string $name) : string {
103
        $request = Request::get($this->GetFileUrl($name))->send();
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
104
        return $response->body;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $response seems to be never defined.
Loading history...
105
    }
106
107
    public function DeleteFile (string $name) {
108
        $url = $this->BuildUrl("/files", [
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
109
            "filename" => $name
110
        ]);
111
112
        Request::delete($this->BuildUrl(""))->send();
113
    }
114
115
}