Passed
Push — master ( a45834...0e9c59 )
by alpha
09:47
created

AliyunOssService::boot()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 53

Duplication

Lines 12
Ratio 22.64 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 12
loc 53
ccs 0
cts 41
cp 0
rs 9.0254
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AlphaSnow\AliyunOss;
4
5
use think\Service as BaseService;
6
use AlphaSnow\AliyunOss\Plugins\PutRemoteFile;
7
use League\Flysystem\Config;
8
use League\Flysystem\Filesystem;
9
use OSS\OssClient;
10
11
class AliyunOssService extends BaseService
12
{
13
14
    public function boot()
15
    {
16
        $appConfig = $this->app->get('config');
17
        if(!$appConfig->has('filesystem.disks.aliyun')){
18
            $filesystemConfig = $appConfig->get('filesystem');
19
            if($appConfig->has('aliyun-oss')){
20
                $filesystemConfig['disks']['aliyun'] = $appConfig->get('aliyun-oss');
21
            }else{
22
                $filesystemConfig['disks']['aliyun'] = require __DIR__.'/config/config.php';
23
            }
24
            $filesystemConfig['disks']['aliyun']['type'] = AliyunOssDriver::class;
25
            $appConfig->set($filesystemConfig,'filesystem');
26
        }
27
28
        $this->app->bind('aliyun.oss.filesystem',function(){
29
            $adapter = $this->app->get('aliyun.oss.adapter');
30
31
            $filesystem = new Filesystem($adapter, new Config(['disable_asserts' => true]));
32
            $filesystem->addPlugin(new PutRemoteFile());
33
34
            return $filesystem;
35
        });
36
37
        $this->app->bind('aliyun.oss.adapter',function(){
38
            $config = $this->app->make(AliyunOssConfig::class);
39
            $client = $this->app->get(OssClient::class);
40
41
            $adapter = new AliyunOssAdapter($client, $config);
42
43
            return $adapter;
44
        });
45
46
        $this->app->bind(AliyunOssConfig::class, function () {
47
            $config = $this->app->get('config')->get('aliyun-oss');
48
            $ossConfig = new AliyunOssConfig($config);
49
            $ossConfig->checkRequired();
50
            return $ossConfig;
51
        });
52
53 View Code Duplication
        $this->app->bind(OssClient::class, function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $ossConfig = $this->app->get(AliyunOssConfig::class);
55
            $ossClient = new OssClient(
56
                $ossConfig->getAccessId(),
57
                $ossConfig->getAccessKey(),
58
                $ossConfig->getOssEndpoint(),
59
                $ossConfig->isCname(),
60
                $ossConfig->getSecurityToken(),
61
                $ossConfig->getRequestProxy()
62
            );
63
            return $ossClient;
64
        });
65
        $this->app->bind( 'aliyun.oss.client',OssClient::class);
66
    }
67
68
    public function register()
69
    {
70
71
    }
72
}
73