GetFederationTokenV3   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 34
dl 0
loc 93
ccs 0
cts 50
cp 0
rs 10
c 1
b 0
f 1
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 3 1
A getDefaultPolicy() 0 30 1
A handle() 0 14 2
A getCustomPolicy() 0 6 1
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->getDefaultPolicy($path);
41
42
        $params = [
43
            'DurationSeconds' => $seconds,
44
            'Name'            => $name,
45
            'Policy'          => urlencode($policy),
46
        ];
47
48
        return $this->request(
49
            $params, 'GetFederationToken', 'sts', '2018-08-13'
50
        );
51
    }
52
53
    /**
54
     * @param Closure $callable
55
     * @param $path
56
     *
57
     * @return string
58
     */
59
    protected function getCustomPolicy(Closure $callable, $path)
60
    {
61
        $policy = call_user_func($callable, $path, $this->getConfig());
62
63
        return \GuzzleHttp\json_encode(
64
            $policy, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
65
        );
66
    }
67
68
    /**
69
     * @see https://cloud.tencent.com/document/product/436/31923
70
     *
71
     * @param $path
72
     *
73
     * @return string
74
     */
75
    protected function getDefaultPolicy($path)
76
    {
77
        $appId = $this->getCredentials()['appId'];
78
79
        $region = $this->getConfig()->get('region');
80
        $bucket = $this->getConfig()->get('bucket');
81
82
        $policy = [
83
            'version'   => '2.0',
84
            'statement' => [
85
                'action' => [
86
                    // 简单上传
87
                    'name/cos:PutObject',
88
                    'name/cos:PostObject',
89
                    // 分片上传
90
                    'name/cos:InitiateMultipartUpload',
91
                    'name/cos:ListParts',
92
                    'name/cos:UploadPart',
93
                    'name/cos:CompleteMultipartUpload',
94
                    'name/cos:AbortMultipartUpload',
95
                ],
96
                'effect'   => 'allow',
97
                'resource' => [
98
                    "qcs::cos:$region:uid/$appId:$bucket-$appId/$path",
99
                ],
100
            ],
101
        ];
102
103
        return \GuzzleHttp\json_encode(
104
            $policy, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
105
        );
106
    }
107
}
108