Passed
Push — master ( ea7aad...a7d002 )
by alpha
02:37
created

AliyunOssAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 69
ccs 18
cts 19
cp 0.9474
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 4 1
A getTemporaryUrl() 0 13 2
1
<?php
2
3
namespace AlphaSnow\AliyunOss;
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
class AliyunOssAdapter extends BaseAdapter implements CanOverwriteFiles
12
{
13
    /**
14
     * @var AliyunOssConfig
15
     */
16
    protected $ossConfig;
17
18
    /**
19
     * @param OssClient $ossClient
20
     * @param AliyunOssConfig $ossConfig
21
     */
22 1
    public function __construct(OssClient $ossClient, AliyunOssConfig $ossConfig)
23
    {
24 1
        $this->ossConfig = $ossConfig;
25 1
        parent::__construct($ossClient, $ossConfig->get('bucket'), $ossConfig->get('prefix', null), $ossConfig->get('options', []));
26 1
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 6
    protected function getOptionsFromConfig(Config $config)
32
    {
33 6
        $options = parent::getOptionsFromConfig($config);
34
35 6
        if ($visibility = $config->get('visibility')) {
36
            // Object ACL > Bucket ACL
37 1
            $options[OssClient::OSS_OBJECT_ACL] = $visibility === AdapterInterface::VISIBILITY_PUBLIC ? OssClient::OSS_ACL_TYPE_PUBLIC_READ : OssClient::OSS_ACL_TYPE_PRIVATE;
38
        }
39
40 6
        return $options;
41
    }
42
43
    /**
44
     * Used by \Illuminate\Filesystem\FilesystemAdapter::url
45
     * Get the URL for the file at the given path.
46
     *
47
     * @param string $path
48
     * @return string
49
     */
50 3
    public function getUrl($path)
51
    {
52 3
        return $this->ossConfig->getUrlDomain().'/' . ltrim($path, '/');
53
    }
54
55
    /**
56
     * Used by \Illuminate\Filesystem\FilesystemAdapter::temporaryUrl
57
     * Get a temporary URL for the file at the given path.
58
     *
59
     * @param string $path
60
     * @param \DateTimeInterface $expiration
61
     * @param array $options
62
     * @return string
63
     *
64
     * @throws \RuntimeException
65
     */
66 2
    public function getTemporaryUrl($path, $expiration, array $options = [])
67
    {
68 2
        $object = $this->applyPathPrefix($path);
69 2
        $clientOptions = $this->getOptionsFromConfig(new Config($options));
70 2
        $timeout = $expiration->getTimestamp() - (new \DateTime('now'))->getTimestamp();
71
72 2
        $url = $this->client->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $clientOptions);
73
        // correct internal domain
74 2
        if ($this->ossConfig->get('internal')) {
75 2
            return str_replace($this->ossConfig->getInternalDomain(), $this->ossConfig->getUrlDomain(), $url);
76
        }
77
        return $url;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $url; (OSS\Http\ResponseCore) is incompatible with the return type documented by AlphaSnow\AliyunOss\Aliy...dapter::getTemporaryUrl of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
78
    }
79
}
80