Passed
Push — master ( 93ae1f...c8123d )
by TQ
04:05
created

Rest::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 0
dl 0
loc 30
ccs 24
cts 24
cp 1
crap 4
rs 9.6333
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
use GuzzleHttp\Psr7\Utils;
11
12
class Rest
13
{
14
    /**
15
     * @var Config
16
     */
17
    protected $config;
18
19
    protected $endpoint;
20
    protected $method;
21
    protected $storagePath;
22
    public $headers = [];
23
24
    /**
25
     * @var Psr7\Stream
26
     */
27
    protected $file;
28
29
30 20
    public function __construct(Config $config)
31
    {
32 20
        $this->config   = $config;
33 20
        $this->endpoint = $config->getProtocol() . Config::$restApiEndPoint . '/' . $config->serviceName;
34
    }
35
36 19
    public function request($method, $storagePath)
37
    {
38 19
        $this->method = strtoupper($method);
39 19
        $this->storagePath = '/' . ltrim($storagePath, '/');
40 19
        return $this;
41
    }
42
43
44
    /**
45
     * @param string|resource $file
46
     *
47
     * @return $this
48
     */
49 14
    public function withFile($file)
50
    {
51 14
        $stream = Utils::streamFor($file);
52 14
        $this->file = $stream;
53
54 14
        return $this;
55
    }
56
57
    /**
58
     * @return mixed|\Psr\Http\Message\ResponseInterface
59
     */
60 19
    public function send()
61
    {
62 19
        $client = new Client([
63 19
            'timeout' => $this->config->timeout,
64 19
        ]);
65
66 19
        $url = $this->endpoint . $this->storagePath;
67 19
        $body = null;
68 19
        if ($this->file && $this->method === 'PUT') {
69 14
            $body = $this->file;
70
        }
71
72 19
        $request = new Psr7\Request(
73 19
            $this->method,
74 19
            Util::encodeURI($url),
75 19
            $this->headers,
76 19
            $body
77 19
        );
78 19
        $authHeader = Signature::getHeaderSign($this->config,
79 19
            $this->method,
80 19
            $request->getUri()->getPath()
81 19
        );
82 19
        foreach ($authHeader as $head => $value) {
83 19
            $request = $request->withHeader($head, $value);
84
        }
85 19
        $response = $client->send($request, [
86 19
            'debug' => $this->config->debug
87 19
        ]);
88
89 17
        return $response;
90
    }
91
92 5
    public function withHeader($header, $value)
93
    {
94 5
        $header = strtolower(trim($header));
95
96 5
        $this->headers[$header] = $value;
97 5
        return $this;
98
    }
99
100 14
    public function withHeaders($headers)
101
    {
102 14
        if (is_array($headers)) {
103 14
            foreach ($headers as $header => $value) {
104 1
                $this->withHeader($header, $value);
105
            }
106
        }
107 14
        return $this;
108
    }
109
110 1
    public function toRequest()
111
    {
112 1
        $url = $this->endpoint . $this->storagePath;
113 1
        $body = null;
114 1
        if ($this->file && $this->method === 'PUT') {
115 1
            $body = $this->file;
116
        }
117
118 1
        $request = new Psr7\Request(
119 1
            $this->method,
120 1
            Util::encodeURI($url),
121 1
            $this->headers,
122 1
            $body
123 1
        );
124 1
        $authHeader = Signature::getHeaderSign($this->config,
125 1
            $this->method,
126 1
            $request->getUri()->getPath()
127 1
        );
128 1
        foreach ($authHeader as $head => $value) {
129 1
            $request = $request->withHeader($head, $value);
130
        }
131 1
        return $request;
132
    }
133
}
134