Passed
Pull Request — master (#12)
by Dmitriy
02:17
created

FileStorageServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
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 23
ccs 0
cts 20
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateAdapter() 0 8 3
A register() 0 10 2
1
<?php
2
3
namespace Yiisoft\Files;
4
5
use League\Flysystem\FilesystemAdapter;
6
use Yiisoft\Di\Container;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Di\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Yiisoft\Di\Support\ServiceProvider;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Di\Support\ServiceProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Yiisoft\Factory\Factory;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Factory\Factory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
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, new Filesystem($adapter, $aliases, $configParams));
22
        }
23
    }
24
25
    private function validateAdapter(string $alias, array $config)
26
    {
27
        $adapter = $config['adapter']['__class'] ?? false;
28
        if (!$adapter) {
29
            throw new \InvalidArgumentException("Adapter is not defined in the '$alias' storage config.");
30
        }
31
        if (!is_subclass_of($adapter, FilesystemAdapter::class)) {
32
            throw new \InvalidArgumentException("Adapter must implements FilesystemAdapterInterface.");
33
        }
34
    }
35
}
36