Passed
Push — master ( 7755b7...9e7351 )
by Bence
02:08
created

Container   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 53.66%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 67
ccs 22
cts 41
cp 0.5366
rs 10
c 0
b 0
f 0
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A SetConfiguration() 0 2 1
A IsRunning() 0 2 1
A GetLastLogId() 0 2 1
A GetConfiguration() 0 2 1
A GetLastLogWebSocketUrl() 0 2 1
A Run() 0 4 1
A Build() 0 4 1
A GetContainerName() 0 2 1
A Delete() 0 2 1
A __construct() 0 4 1
A FetchStatus() 0 3 1
A SetContainerName() 0 2 1
A UpdateChanges() 0 3 1
A GetId() 0 2 1
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
    public function FetchStatus () {
21 2
        $request = Request::get("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey()))->expectsText()->send();
22 2
        $this->statusCache = json_decode($request->body, true);
23 2
    }
24
25 2
    public function GetId () : string {
26 2
        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 2
    public function UpdateChanges () {
38 2
        Request::put("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey()))
39 2
            ->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send();
40 2
    }
41
42 2
    public function GetContainerName () : string {
43 2
        return $this->statusCache["configuration"]["name"];
44
    }
45
46 2
    public function SetContainerName (string $newname) {
47 2
        $this->statusCache["configuration"]["name"] = $newname;
48 2
    }
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 2
    public function Delete () {
75 2
        Request::delete("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey()))->send();
76 2
    }
77
78
}