Completed
Pull Request — master (#267)
by
unknown
08:50
created

Mac::MACToken()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 48
nop 4
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
1
<?php
2
namespace QiniuRtc;
3
4
class Mac
5
{
6
    public $_accessKey;
7
    public $_secretKey;
8
9
    public function __construct($accessKey, $secretKey)
10
    {
11
        $this->_accessKey = $accessKey;
12
        $this->_secretKey = $secretKey;
13
    }
14
15
    public function MACToken($method, $url, $contentType, $body)
16
    {
17
        $url = parse_url($url);
18
        $data = '';
19
        if (!empty($url['path'])) {
20
            $data = $method . ' ' . $url['path'];
21
        }
22
        if (!empty($url['query'])) {
23
            $data .= '?' . $url['query'];
24
        }
25
        if (!empty($url['host'])) {
26
            $data .= "\nHost: " . $url['host'];
27
            if (isset($url['port'])) {
28
                $data .= ':' . $url['port'];
29
            }
30
        }
31
        if (!empty($contentType)) {
32
            $data .= "\nContent-Type: " . $contentType;
33
        }
34
        $data .= "\n\n";
35
        if (!empty($body)) {
36
            $data .= $body;
37
        }
38
        return 'Qiniu ' . $this->_accessKey . ':' . Utils::sign($this->_secretKey, $data);
39
    }
40
}
41