Passed
Pull Request — master (#36)
by sabaku
21:46
created

Rest::withHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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
    /**
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 15
    public function __construct(Config $config)
30
    {
31 15
        $this->config   = $config;
32 15
        $this->endpoint = $config->getProtocol() . Config::$restApiEndPoint . '/' . $config->bucketName;
33 15
    }
34
35 14
    public function request($method, $storagePath)
36
    {
37 14
        $this->method = strtoupper($method);
38 14
        $this->storagePath = '/' . ltrim($storagePath, '/');
39 14
        return $this;
40 1
    }
41
42
43
    /**
44
     * @param string|resource $file
45
     *
46
     * @return $this
47
     */
48 9
    public function withFile($file)
49
    {
50 9
        $stream = Psr7\stream_for($file);
51 9
        $this->file = $stream;
52
53 9
        return $this;
54
    }
55
56
    /**
57
     * @return mixed|\Psr\Http\Message\ResponseInterface
58
     */
59 14
    public function send()
60
    {
61 14
        $client = new Client([
62 14
            'timeout' => $this->config->timeout,
63 14
        ]);
64
65 14
        $url = $this->endpoint . $this->storagePath;
66 14
        $body = null;
67 14
        if ($this->file && $this->method === 'PUT') {
68 9
            $body = $this->file;
69 9
        }
70
71 14
        $request = new Psr7\Request(
72 14
            $this->method,
73 14
            Util::encodeURI($url),
74 14
            $this->headers,
75
            $body
76 14
        );
77 14
        $authHeader = Signature::getHeaderSign($this->config,
78 14
            $this->method,
79 14
            $request->getUri()->getPath()
80 14
        );
81 14
        foreach ($authHeader as $head => $value) {
82 14
            $request = $request->withHeader($head, $value);
83 14
        }
84 14
        $response = $client->send($request, [
85 14
            'debug' => $this->config->debug
86 14
        ]);
87
88 12
        return $response;
89
    }
90
91 2
    public function withHeader($header, $value)
92
    {
93 2
        $header = strtolower(trim($header));
94
95 2
        $this->headers[$header] = $value;
96 2
        return $this;
97
    }
98
99 9
    public function withHeaders($headers)
100
    {
101 9
        if (is_array($headers)) {
102 9
            foreach ($headers as $header => $value) {
103
                $this->withHeader($header, $value);
104 9
            }
105 9
        }
106 9
        return $this;
107
    }
108
}
109