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

AzureAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 15 4
A displayName() 0 3 1
A prepareAdapter() 0 9 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 Yii;
11
use yii\base\InvalidConfigException;
12
use yuncms\filesystem\Adapter;
13
use MicrosoftAzure\Storage\Common\ServicesBuilder;
14
15
/**
16
 * Class AzureAdapter
17
 *
18
 * @author Tongle Xu <[email protected]>
19
 * @since 3.0
20
 */
21
class AzureAdapter extends Adapter
22
{
23
    /**
24
     * @var string
25
     */
26
    public $accountName;
27
    /**
28
     * @var string
29
     */
30
    public $accountKey;
31
    /**
32
     * @var string
33
     */
34
    public $container;
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function init()
40
    {
41
        if ($this->accountName === null) {
42
            throw new InvalidConfigException('The "accountName" property must be set.');
43
        }
44
45
        if ($this->accountKey === null) {
46
            throw new InvalidConfigException('The "accountKey" property must be set.');
47
        }
48
49
        if ($this->container === null) {
50
            throw new InvalidConfigException('The "container" property must be set.');
51
        }
52
53
        parent::init();
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public static function displayName(): string
60
    {
61
        return Yii::t('yuncms', 'Microsoft Azure');
62
    }
63
64
    /**
65
     * @return \League\Flysystem\Azure\AzureAdapter
66
     */
67
    protected function prepareAdapter()
68
    {
69
        return new \League\Flysystem\Azure\AzureAdapter(
70
            ServicesBuilder::getInstance()->createBlobService(sprintf(
71
                'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s',
72
                base64_encode($this->accountName),
73
                base64_encode($this->accountKey)
74
            )),
75
            $this->container
76
        );
77
    }
78
}