MainConfig   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 4
dl 0
loc 108
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getCacheConfig() 0 4 1
A hasFileSystemConfig() 0 4 1
A hasAdaptorConfig() 0 4 1
A hasCacheConfig() 0 4 1
A buildAdaptorConfigs() 0 6 2
B validateConfigAndSetDefaults() 0 24 6
A getFileSystemConfig() 0 4 1
A getAdaptorConfig() 0 4 1
A buildCacheConfigs() 0 6 2
A buildFileSystemConfigs() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem\Config;
5
6
use WShafer\PSR11FlySystem\Exception\MissingConfigException;
7
8
class MainConfig
9
{
10
    protected $config = [];
11
12
    protected $adaptors = [];
13
14
    protected $cache = [];
15
16
    protected $fileSystems = [];
17
18 24
    public function __construct(array $config)
19
    {
20 24
        $this->validateConfigAndSetDefaults($config);
21 23
        $this->config = $config;
22 23
        $this->buildAdaptorConfigs();
23 23
        $this->buildFileSystemConfigs();
24 23
        $this->buildCacheConfigs();
25 23
    }
26
27 24
    public function validateConfigAndSetDefaults(&$config)
28
    {
29 24
        if (empty($config)
30 24
            || empty($config['flysystem'])
31
        ) {
32 3
            throw new MissingConfigException(
33 3
                'No config key of "flysystem" found in config array.'
34
            );
35
        }
36
37 23
        if (empty($config['flysystem']['adaptors'])) {
38 1
            throw new MissingConfigException(
39 1
                'No config key of "adaptors" found in flysystem config array.'
40
            );
41
        }
42
43 23
        if (empty($config['flysystem']['fileSystems']['default'])) {
44 23
            $config['flysystem']['fileSystems']['default'] = [];
45
        }
46
47 23
        if (empty($config['flysystem']['caches']['default'])) {
48 23
            $config['flysystem']['caches']['default']['type'] = 'memory';
49
        }
50 23
    }
51
52
    /**
53
     * @param $fileSystem
54
     * @return FileSystemConfig|null
55
     */
56 2
    public function getFileSystemConfig($fileSystem)
57
    {
58 2
        return $this->fileSystems[$fileSystem] ?? null;
59
    }
60
61
    /**
62
     * @param $adaptor
63
     * @return AdaptorConfig|null
64
     */
65 2
    public function getAdaptorConfig($adaptor)
66
    {
67 2
        return $this->adaptors[$adaptor] ?? null;
68
    }
69
70
    /**
71
     * @param $cache
72
     *
73
     * @return CacheConfig|null
74
     */
75 3
    public function getCacheConfig($cache)
76
    {
77 3
        return $this->cache[$cache] ?? null;
78
    }
79
80 2
    public function hasFileSystemConfig($fileSystem) : bool
81
    {
82 2
        return key_exists($fileSystem, $this->fileSystems);
83
    }
84
85 2
    public function hasAdaptorConfig($adaptor) : bool
86
    {
87 2
        return key_exists($adaptor, $this->adaptors);
88
    }
89
90 2
    public function hasCacheConfig($cache) : bool
91
    {
92 2
        return key_exists($cache, $this->cache);
93
    }
94
95 23
    protected function buildAdaptorConfigs()
96
    {
97 23
        foreach ($this->config['flysystem']['adaptors'] as $name => $adaptor) {
98 23
            $this->adaptors[$name] = new AdaptorConfig($adaptor);
99
        }
100 23
    }
101
102 23
    protected function buildCacheConfigs()
103
    {
104 23
        foreach ($this->config['flysystem']['caches'] as $name => $cache) {
105 23
            $this->cache[$name] = new CacheConfig($cache);
106
        }
107 23
    }
108
109 23
    protected function buildFileSystemConfigs()
110
    {
111 23
        foreach ($this->config['flysystem']['fileSystems'] as $name => $fileSystem) {
112 23
            $this->fileSystems[$name] = new FileSystemConfig($fileSystem);
113
        }
114 23
    }
115
}
116