Passed
Pull Request — master (#54)
by
unknown
10:38
created

Rest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 122
ccs 68
cts 68
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 6

7 Methods

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