AliyunOssAdapter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 3
dl 0
loc 74
ccs 8
cts 24
cp 0.3333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOptionsFromConfig() 0 11 3
A getUrl() 0 19 3
A getTemporaryUrl() 0 8 1
1
<?php
2
3
namespace AlphaSnow\AliyunOssThink;
4
5
use Aliyun\Flysystem\AliyunOss\AliyunOssAdapter as BaseAdapter;
6
use League\Flysystem\Adapter\CanOverwriteFiles;
7
use League\Flysystem\AdapterInterface;
8
use League\Flysystem\Config;
9
use OSS\OssClient;
10
11
/**
12
 * Class AliyunOssAdapter
13
 * @package AlphaSnow\AliyunOssThink
14
 */
15
class AliyunOssAdapter extends BaseAdapter implements CanOverwriteFiles
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $config;
21
22 2
    public function __construct(OssClient $client, array $config)
23
    {
24 2
        $this->config = $config;
25 2
        parent::__construct($client, $config['bucket'], $config['prefix'], $config['options']);
26 2
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    protected function getOptionsFromConfig(Config $config)
32
    {
33 1
        $options = parent::getOptionsFromConfig($config);
34
35 1
        if ($visibility = $config->get('visibility')) {
36
            // Object ACL > Bucket ACL
37
            $options[OssClient::OSS_OBJECT_ACL] = $visibility === AdapterInterface::VISIBILITY_PUBLIC ? OssClient::OSS_ACL_TYPE_PUBLIC_READ : OssClient::OSS_ACL_TYPE_PRIVATE;
38
        }
39
40 1
        return $options;
41
    }
42
43
    /**
44
     * Used by \Illuminate\Filesystem\FilesystemAdapter::url
45
     *
46
     * @param string $path
47
     * @return string
48
     */
49
    public function getUrl($path)
50
    {
51
        $url = '';
52
53
        if ($this->config['is_ssl']) {
54
            $url .= 'https://';
55
        } else {
56
            $url .= 'http://';
57
        }
58
59
        if ($this->config['is_cname']) {
60
            $url .= $this->config['cdn_domain'];
61
        } else {
62
            $url .= $this->config['bucket'] . '.' . $this->config['endpoint'];
63
        }
64
65
        $url .= '/' . ltrim($path, '/');
66
        return $url;
67
    }
68
69
    /**
70
     * Used by \Illuminate\Filesystem\FilesystemAdapter::temporaryUrl
71
     * Get a temporary URL for the file at the given path.
72
     *
73
     * @param string $path
74
     * @param \DateTimeInterface $expiration
75
     * @param array $options
76
     * @return string
77
     *
78
     * @throws \RuntimeException
79
     */
80
    public function getTemporaryUrl($path, $expiration, array $options = [])
81
    {
82
        $object = $this->applyPathPrefix($path);
83
        $clientOptions = $this->getOptionsFromConfig(new Config($options));
84
        $timeout = $expiration->getTimestamp() - time();
85
86
        return $this->client->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $clientOptions);
87
    }
88
}
89