Passed
Push — master ( 62a894...f6c571 )
by Bence
02:04
created

Container::UploadDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 6
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 CSFCloud\Files\RecursiveFileListing;
8
use Httpful\Request;
9
10
class Container extends Resource {
11
12
    private $containerId;
13
    private $statusCache;
14
15 2
    public function __construct (KeyManager $km, string $id) {
16 2
        parent::__construct($km);
17 2
        $this->containerId = $id;
18 2
        $this->FetchStatus();
19 2
    }
20
21 2
    private function BuildUrl (string $api, array $query = []) : string {
22 2
        $query["key"] = $this->keymanager->GetServerKey();
23 2
        $url = "https://api.csfcloud.com/container/" . urlencode($this->containerId) . $api . "?" . http_build_query($query);
24
        //echo "URL: " . $url . PHP_EOL;
25 2
        return $url;
26
    }
27
28 2
    public function FetchStatus () {
29 2
        $request = Request::get($this->BuildUrl(""))->expectsText()->send();
30 2
        $this->statusCache = json_decode($request->body, true);
31 2
    }
32
33 2
    public function GetId () : string {
34 2
        return $this->containerId;
35
    }
36
37
    public function GetConfiguration () : array {
38
        return $this->statusCache["configuration"];
39
    }
40
41
    public function SetConfiguration (array $cnf) {
42
        $this->statusCache["configuration"] = $cnf;
43
    }
44
45 2
    public function UpdateChanges () {
46 2
        Request::put($this->BuildUrl(""))
47 2
            ->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send();
48 2
    }
49
50 2
    public function GetContainerName () : string {
51 2
        return $this->statusCache["configuration"]["name"];
52
    }
53
54 2
    public function SetContainerName (string $newname) {
55 2
        $this->statusCache["configuration"]["name"] = $newname;
56 2
    }
57
58
    public function IsRunning () : bool {
59
        return $this->statusCache["running"];
60
    }
61
62
    public function GetLastLogId () : ?string {
63
        return $this->statusCache["last_log"];
64
    }
65
66
    public function GetLastLogWebSocketUrl () : string {
67
        return "wss://dashboard-logs.csfcloud.com/log?id=" . $this->statusCache["last_log"];
68
    }
69
70
    public function Build () : string {
71
        $response = Request::get($this->BuildUrl("/build"))->expectsJson()->send();
72
        if (isset($response->body->run_id)) {
73
            var_dump($response->body);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($response->body) looks like debug code. Are you sure you do not want to remove it?
Loading history...
74
            throw new Exception("Missing run_id in response");
0 ignored issues
show
Bug introduced by
The type CSFCloud\Containers\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
75
        }
76
        $this->statusCache["last_log"] = $response->body->run_id;
77
        return $this->statusCache["last_log"];
78
    }
79
80
    public function Run () : string {
81
        $response = Request::get($this->BuildUrl("/run"))->expectsJson()->send();
82
        if (isset($response->body->run_id)) {
83
            var_dump($response->body);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($response->body) looks like debug code. Are you sure you do not want to remove it?
Loading history...
84
            throw new Exception("Missing run_id in response");
85
        }
86
        $this->statusCache["last_log"] = $response->body->run_id;
87
        return $this->statusCache["last_log"];
88
    }
89
90
    public function Stop () {
91
        Request::get($this->BuildUrl("/stop"))->expectsJson()->send();
92
        $this->statusCache["last_log"] = null;
93
    }
94
95 2
    public function Delete () {
96 2
        Request::delete($this->BuildUrl(""))->send();
97 2
    }
98
99 2
    private function GetFileUrl(string $name) : string {
100 2
        return $this->BuildUrl("/files", [
101 2
            "filename" => $name
102
        ]);
103
    }
104
105 2
    public function UploadFile (string $file, string $name) {
106 2
        Request::post($this->GetFileUrl($name))->body(file_get_contents($file))->send();
107 2
    }
108
109
    public function DownloadFile (string $name, string $file) {
110
        shell_exec("wget " . escapeshellarg($this->GetFileUrl($name)) . " -O " . escapeshellarg($file));
111
    }
112
113 2
    public function GetFileContents (string $name) : string {
114 2
        $response = Request::get($this->GetFileUrl($name))->send();
115 2
        return $response->body;
116
    }
117
118
    public function DeleteFile (string $name) {
119
        $url = $this->BuildUrl("/files", [
120
            "filename" => $name
121
        ]);
122
123
        Request::delete($url)->send();
124
    }
125
126
    public function UploadDirectory (string $localdir, string $remotedir) {
127
        $this->DeleteFile($remotedir);
128
129
        $localdir = realpath($localdir);
130
        $filelister = new RecursiveFileListing($localdir);
131
        $files = $filelister->scan();
132
133
        foreach ($files as $file) {
134
            $relativefile = str_replace($localdir, "", $file);
135
            $this->UploadFile($relativefile, $file);
136
        }
137
    }
138
139
}