Passed
Push — master ( ff99dc...8a1a6a )
by Alexander
01:48 queued 13s
created

FileStorageServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 20
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 2
A validateAdapter() 0 9 3
1
<?php
2
3
namespace Yiisoft\Yii\Filesystem;
4
5
use League\Flysystem\FilesystemAdapter;
6
use Yiisoft\Di\Container;
7
use Yiisoft\Di\Support\ServiceProvider;
8
use Yiisoft\Factory\Factory;
9
10
final class FileStorageServiceProvider extends ServiceProvider
11
{
12
    public function register(Container $container): void
13
    {
14
        $factory = new Factory();
15
        $configs = $container->get(FileStorageConfigs::class)->getConfigs();
16
        foreach ($configs as $alias => $config) {
17
            $this->validateAdapter($alias, $config);
18
            $configParams = $config['config'] ?? [];
19
            $aliases = $config['aliases'] ?? [];
20
            $adapter = $factory->create($config['adapter']);
21
            $container->set($alias, fn () => new Filesystem($adapter, $aliases, $configParams));
22
        }
23
    }
24
25
    private function validateAdapter(string $alias, array $config): void
26
    {
27
        $adapter = $config['adapter']['__class'] ?? false;
28
        if (!$adapter) {
29
            throw new \RuntimeException("Adapter is not defined in the \"$alias\" storage config.");
30
        }
31
32
        if (!$adapter instanceof FilesystemAdapter) {
33
            throw new \RuntimeException('Adapter must implement \League\Flysystem\FilesystemAdapter interface.');
34
        }
35
    }
36
}
37