Test Failed
Push — master ( 267017...2fc114 )
by alpha
03:07
created

ServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace AlphaSnow\AliyunOss;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7
use Aliyun\Flysystem\AliyunOss\Plugins\PutFile;
8
use League\Flysystem\Config;
9
use League\Flysystem\Filesystem;
10
use OSS\OssClient;
11
12
/**
13
 * Class ServiceProvider
14
 * @package AlphaSnow\AliyunOss
15
 */
16
class ServiceProvider extends BaseServiceProvider
17
{
18
    public function boot()
19
    {
20
        $this->mergeConfigFrom(
21
            __DIR__.'/config/config.php',
22
            'filesystems.disks.aliyun'
23
        );
24
25
        $this->app->make('filesystem')
26
            ->extend('aliyun', function ($app, array $config) {
27
                $client = $this->makeOssClient($app, $config);
28
                $adapter = new AliyunOssAdapter($client, $config);
29
                $filesystem = new Filesystem($adapter, new Config(['disable_asserts' => true]));
30
                $filesystem->addPlugin(new PutFile());
31
                return $filesystem;
32
            });
33
    }
34
35
    /**
36
     * @param Application $app
37
     * @param array $config
38
     * @return OssClient
39
     */
40
    protected function makeOssClient($app, $config)
41
    {
42
        return $app->make(OssClient::class, [
43
            'accessKeyId' => $config['accessId'],
44
            'accessKeySecret' => $config['accessKey'],
45
            'endpoint' => $config['endpoint'],
46
            'isCName' => $config['isCname'],
47
            'securityToken' => $config['securityToken']
48
        ]);
49
    }
50
51
    public function register()
52
    {
53
        $this->app->singleton('aliyun-oss.client', function ($app) {
54
            $config = $app->get('config')->get('filesystems.disks.aliyun');
55
            return $this->makeOssClient($app, $config);
56
        });
57
    }
58
}
59