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

AliyunOssService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 19.35 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 6
dl 12
loc 62
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 12 53 3
A register() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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