Completed
Push — master ( 005ddd...9800f2 )
by frey
04:38
created

GetFederationTokenV3::getCanonicalRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 12
cp 0
crap 2
rs 9.9332
1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv5\Plugins;
4
5
use Closure;
6
use Freyo\Flysystem\QcloudCOSv5\Plugins\Traits\TencentCloudAuthV3;
7
use League\Flysystem\Plugin\AbstractPlugin;
8
9
/**
10
 * Class GetFederationToken.
11
 */
12
class GetFederationTokenV3 extends AbstractPlugin
13
{
14
    use TencentCloudAuthV3;
15
16
    /**
17
     * Get the method name.
18
     *
19
     * @return string
20
     */
21
    public function getMethod()
22
    {
23
        return 'getFederationTokenV3';
24
    }
25
26
    /**
27
     * @see https://cloud.tencent.com/document/product/598/33416
28
     *
29
     * @param string $path
30
     * @param int $seconds
31
     * @param Closure $customPolicy
32
     * @param string $name
33
     *
34
     * @return bool|array
35
     */
36
    public function handle($path = '*', $seconds = 7200, Closure $customPolicy = null, $name = 'cos')
37
    {
38
        $policy = !is_null($customPolicy)
39
            ? $this->getCustomPolicy($customPolicy, $path)
40
            : $this->getPolicy($path);
41
42
        $params = [
43
            'DurationSeconds' => $seconds,
44
            'Name' => $name,
45
            'Policy' => urlencode($policy),
46
        ];
47
48
        return $this->request($params, 'GetFederationToken', 'sts');
49
    }
50
51
    /**
52
     * @param Closure $callable
53
     * @param $path
54
     *
55
     * @return string
56
     */
57
    protected function getCustomPolicy(Closure $callable, $path)
58
    {
59
        $policy = call_user_func($callable, $path, $this->getConfig());
60
61
        return \GuzzleHttp\json_encode(
62
            $policy, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
63
        );
64
    }
65
66
    /**
67
     * @see https://cloud.tencent.com/document/product/436/31923
68
     *
69
     * @param $path
70
     *
71
     * @return string
72
     */
73
    protected function getPolicy($path)
74
    {
75
        $appId = $this->getCredentials()['appId'];
76
77
        $region = $this->getConfig()->get('region');
78
        $bucket = $this->getConfig()->get('bucket');
79
80
        $policy = [
81
            'version' => '2.0',
82
            'statement' => [
83
                'action' => [
84
                    // 简单上传
85
                    'name/cos:PutObject',
86
                    'name/cos:PostObject',
87
                    // 分片上传
88
                    'name/cos:InitiateMultipartUpload',
89
                    'name/cos:ListParts',
90
                    'name/cos:UploadPart',
91
                    'name/cos:CompleteMultipartUpload',
92
                    'name/cos:AbortMultipartUpload',
93
                ],
94
                'effect' => 'allow',
95
                'resource' => [
96
                    "qcs::cos:$region:uid/$appId:prefix//$appId/$bucket/$path",
97
                ],
98
            ],
99
        ];
100
101
        return \GuzzleHttp\json_encode(
102
            $policy, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
103
        );
104
    }
105
}
106