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

FileStorageServiceProvider::validateAdapter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
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 8
ccs 0
cts 8
cp 0
crap 12
rs 10
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