Completed
Push — master ( 5289b1...66334b )
by Xu
06:18
created

CosV4Adapter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 21 7
A displayName() 0 3 1
A prepareAdapter() 0 16 1
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\filesystem\adapters;
9
10
use QCloud\Cos\Api;
11
use Yii;
12
use yii\base\InvalidConfigException;
13
use yuncms\filesystem\Adapter;
14
15
/**
16
 * Class CosV4Adapter
17
 *
18
 * @author Tongle Xu <[email protected]>
19
 * @since 3.0
20
 */
21
class CosV4Adapter extends Adapter
22
{
23
    public $protocol = 'http';
24
    public $appId;
25
    public $accessId;
26
    public $accessSecret;
27
    public $bucket;
28
    public $domain;
29
    public $region;
30
    public $timeout = 60;
31
    public $debug = false;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function init()
37
    {
38
        if ($this->appId === null) {
39
            throw new InvalidConfigException('The "appId" property must be set.');
40
        }
41
        if ($this->accessId === null) {
42
            throw new InvalidConfigException('The "accessId" property must be set.');
43
        }
44
        if ($this->accessSecret === null) {
45
            throw new InvalidConfigException('The "accessSecret" property must be set.');
46
        }
47
        if ($this->bucket === null) {
48
            throw new InvalidConfigException('The "bucket" property must be set.');
49
        }
50
        if ($this->domain === null) {
51
            throw new InvalidConfigException('The "domain" property must be set.');
52
        }
53
        if ($this->region === null) {
54
            throw new InvalidConfigException('The "region" property must be set.');
55
        }
56
        parent::init();
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public static function displayName(): string
63
    {
64
        return Yii::t('yuncms', 'QCloud COS');
65
    }
66
67
    /**
68
     * 准备适配器
69
     * @return \Freyo\Flysystem\QcloudCOSv4\Adapter
70
     * @throws \Exception
71
     */
72
    protected function prepareAdapter()
73
    {
74
        $config = [
75
            'protocol' => $this->protocol,
76
            'domain' => $this->domain,
77
            'app_id' => $this->appId,
78
            'secret_id' => $this->accessId,
79
            'secret_key' => $this->accessSecret,
80
            'timeout' => $this->timeout,
81
            'bucket' => $this->bucket,
82
            'region' => $this->region,
83
            'debug' => $this->debug,
84
        ];
85
        $cosApi = new Api($config);
86
87
        return new \Freyo\Flysystem\QcloudCOSv4\Adapter($cosApi,$config);
88
    }
89
}