Completed
Push — master ( c8a547...8676e4 )
by sabaku
03:53
created

Rest::send()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 8
nop 0
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Upyun\Api;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7;
7
use Upyun\Config;
8
use Upyun\Signature;
9
use Upyun\Util;
10
11
class Rest {
12
    /**
13
     * @var Config
14
     */
15
    protected $config;
16
17
    protected $endpoint;
18
    protected $method;
19
    protected $storagePath;
20
    public $headers = [];
21
22
    /**
23
     * @var Psr7\Stream
24
     */
25
    protected $file;
26
27
28
    public function __construct(Config $config) {
29
        $this->config   = $config;
30
        $this->endpoint = Config::$restApiEndPoint . '/' . $config->bucketName;
0 ignored issues
show
Bug introduced by
The property restApiEndPoint cannot be accessed from this context as it is declared private in class Upyun\Config.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
31
    }
32
33
    public function request($method, $storagePath) {
34
        $this->method = strtoupper($method);
35
        $this->storagePath = '/' . ltrim($storagePath, '/');
36
        return $this;
37
    }
38
39
40
    /**
41
     * @param string|resource $file
42
     *
43
     * @return $this
44
     */
45
    public function withFile($file) {
46
        $stream = Psr7\stream_for($file);
47
        $this->file = $stream;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return mixed|\Psr\Http\Message\ResponseInterface
54
     */
55
    public function send() {
56
        $client = new Client([
57
            'timeout' => $this->config->timeout,
58
        ]);
59
60
        $url = ($this->config->useSsl ? 'https://' : 'http://') . $this->endpoint . $this->storagePath;
61
        $body = null;
62
        if($this->file && $this->method === 'PUT') {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
63
            $body = $this->file;
64
        }
65
66
        $request = new Psr7\Request(
67
            $this->method,
68
            Util::encodeURI($url),
69
            $this->headers,
70
            $body
71
        );
72
        $authHeader = Signature::getHeaderSign($this->config,
73
            $this->method,
74
            $request->getUri()->getPath()
75
        );
76
        foreach($authHeader as $head => $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
77
            $request = $request->withHeader($head, $value);
78
        }
79
        $response = $client->send($request, [
80
            'debug' => $this->config->debug
81
        ]);
82
83
        return $response;
84
    }
85
86
    public function withHeader($header, $value) {
87
        $header = strtolower(trim($header));
88
89
        $this->headers[$header] = $value;
90
        return $this;
91
    }
92
93
    public function withHeaders($headers) {
94
        if(is_array($headers)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
95
            foreach ($headers as $header => $value) {
96
                $this->withHeader($header, $value);
97
            }
98
        }
99
        return $this;
100
    }
101
}
102