Passed
Push — master ( dc4af7...7bb9d6 )
by alpha
15:00
created

AliyunServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 42
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMicros() 0 7 3
A boot() 0 13 1
1
<?php
2
3
namespace AlphaSnow\LaravelFilesystem\Aliyun;
4
5
use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
6
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
7
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
8
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
9
use Illuminate\Container\Container;
10
use Illuminate\Filesystem\FilesystemAdapter;
11
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
12
use League\Flysystem\Filesystem;
13
14
class AliyunServiceProvider extends BaseServiceProvider
15
{
16
    /**
17
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
18
     */
19
    public function boot(): void
20
    {
21
        $this->mergeConfigFrom(
22
            __DIR__ . '/../config/config.php',
23
            'filesystems.disks.oss'
24
        );
25
26
        $this->app->make('filesystem')
27
            ->extend('oss', function ($app, array $config) {
28
                $adapter = (new AliyunFactory())->createAdapter($config);
29
                $filesystemAdapter = new FilesystemAdapter(new Filesystem($adapter), $adapter, $config);
30
                $this->registerMicros($config['macros'] ?? [], $filesystemAdapter, $app);
31
                return $filesystemAdapter;
32
            });
33
    }
34
35
    /**
36
     * @var string[]
37
     */
38
    protected $defaultMacroClasses = [
39
        AppendFile::class,
40
        AppendObject::class,
41
    ];
42
43
    /**
44
     * @param array $macroClasses
45
     * @param FilesystemAdapter $filesystemAdapter
46
     * @param Container $app
47
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
48
     */
49
    protected function registerMicros(array $macroClasses, FilesystemAdapter $filesystemAdapter, Container $app): void
50
    {
51
        $macroClasses = array_merge($this->defaultMacroClasses, $macroClasses);
52
        foreach ($macroClasses as $macroClass) {
53
            $macro = $app->make($macroClass);
54
            if ($macro instanceof AliyunMacro) {
55
                $filesystemAdapter::macro($macro->name(), $macro->macro());
56
            }
57
        }
58
    }
59
}
60