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

FileStorageServiceProvider::validateAdapter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 12
rs 10
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