AliyunOssService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 57
ccs 36
cts 40
cp 0.9
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setupConfig() 0 10 2
A boot() 0 4 1
A register() 0 39 1
1
<?php
2
3
namespace AlphaSnow\AliyunOssThink;
4
5
use think\Cache;
6
use think\Service as BaseService;
7
use Aliyun\Flysystem\AliyunOss\Plugins\PutFile;
8
use League\Flysystem\Config;
9
use League\Flysystem\Filesystem;
10
use OSS\OssClient;
11
12
class AliyunOssService extends BaseService
13
{
14 2
    protected function setupConfig()
15
    {
16 2
        $appConfig = $this->app->get('config');
17 2
        if (!$appConfig->has('filesystem.disks.aliyun')) {
18 2
            $filesystemConfig = $appConfig->get('filesystem');
19 2
            $filesystemConfig['disks']['aliyun'] = require __DIR__.'/config/config.php';
20 2
            $filesystemConfig['disks']['aliyun']['type'] = AliyunOssDriver::class;
21 2
            $appConfig->set($filesystemConfig, 'filesystem');
22
        }
23 2
    }
24 2
    public function boot()
25
    {
26 2
        $this->setupConfig();
27 2
    }
28
29
    public function register()
30
    {
31 2
        $this->app->bind(AliyunOssDriver::class, function () {
32 1
            $cache = $this->app->get(Cache::class);
33 1
            $config = $this->app->get('config')->get('filesystem.disks.aliyun');
34 1
            $driver = new AliyunOssDriver($cache, $config);
35 1
            return $driver;
36 2
        });
37 2
        $this->app->bind('aliyun-oss.driver', AliyunOssDriver::class);
38
39 2
        $this->app->bind('aliyun-oss.filesystem', function () {
40
            $adapter = $this->app->get('aliyun-oss.adapter');
41
42
            $filesystem = new Filesystem($adapter, new Config(['disable_asserts' => true]));
43
            $filesystem->addPlugin(new PutFile());
44
45
            return $filesystem;
46 2
        });
47
48 2
        $this->app->bind('aliyun-oss.adapter', function () {
49 2
            $config = $this->app->get('config')->get('filesystem.disks.aliyun');
50 2
            $client = $this->app->get('aliyun-oss.client');
51
52 2
            $adapter = new AliyunOssAdapter($client, $config);
53
54 2
            return $adapter;
55 2
        });
56
57 2
        $this->app->bind('aliyun-oss.client', function () {
58 1
            $config = $this->app->get('config')->get('filesystem.disks.aliyun');
59 1
            return new OssClient(
60 1
                $config['access_id'],
61 1
                $config['access_key'],
62 1
                $config['endpoint'],
63 1
                $config['is_cname'],
64 1
                $config['security_token']
65
            );
66 2
        });
67 2
    }
68
}
69