Completed
Push — master ( 356279...28992d )
by Dmitry
02:58
created

Storage::upload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 7
cp 0
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Basis;
4
5
use GuzzleHttp\Client;
6
7
class Storage
8
{
9
    public $hostname = 'storage';
10
11
    public function download(string $hash)
12
    {
13
        return file_get_contents($this->url($hash));
14
    }
15
16
    public function upload(string $filename, $contents) : string
17
    {
18
        $client = new Client();
19
        $response = $client->request('POST', "http://$this->hostname/storage/upload", [
20
            'multipart' => [
21
                [
22
                    'name' => 'file',
23
                    'filename' => $filename,
24
                    'contents' => $contents,
25
                ],
26
            ]
27
        ]);
28
29
        return json_decode($response->getBody())->hash[0];
30
    }
31
32
    public function url(string $hash) : string
33
    {
34
        return "http://$this->hostname/storage/get?=$hash";
35
    }
36
}
37