Test Failed
Push — master ( 0863bf...7755b7 )
by Bence
02:07
created

Container::GetLastLogId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
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
    public function __construct (KeyManager $km, string $id) {
15
        parent::__construct($km);
16
        $this->containerId = $id;
17
        $this->FetchStatus();
18
    }
19
20
    public function FetchStatus () {
21
        $request = Request::get("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey()))->expectsText()->send();
22
        $this->statusCache = json_decode($request->body, true);
23
    }
24
25
    public function GetId () : string {
26
        return $this->containerId;
27
    }
28
29
    public function GetConfiguration () : array {
30
        return $this->statusCache["configuration"];
31
    }
32
33
    public function SetConfiguration (array $cnf) {
34
        $this->statusCache["configuration"] = $cnf;
35
    }
36
37
    public function UpdateChanges () {
38
        Request::put("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey()))
39
            ->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send();
40
    }
41
42
    public function GetContainerName () : string {
43
        return $this->statusCache["configuration"]["name"];
44
    }
45
46
    public function SetContainerName (string $newname) {
47
        $this->statusCache["configuration"]["name"] = $newname;
48
    }
49
50
    public function IsRunning () : bool {
51
        return $this->statusCache["running"];
52
    }
53
54
    public function GetLastLogId () : ?string {
55
        return $this->statusCache["last_log"];
56
    }
57
58
    public function GetLastLogWebSocketUrl () : string {
59
        return "wss://dashboard-logs.csfcloud.com/log?id=" . $this->statusCache["last_log"];
60
    }
61
62
    public function Build () : string {
63
        $response = Request::get("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "/build?key=" . urlencode($this->keymanager->GetServerKey()))->expectsJson()->send();
64
        $this->statusCache["last_log"] = $response->body->run_id;
65
        return $this->statusCache["last_log"];
66
    }
67
68
    public function Run () : string {
69
        $response = Request::get("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "/run?key=" . urlencode($this->keymanager->GetServerKey()))->expectsJson()->send();
70
        $this->statusCache["last_log"] = $response->body->run_id;
71
        return $this->statusCache["last_log"];
72
    }
73
74
    public function Delete () {
75
        Request::delete("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey()))->send();
76
    }
77
78
}