Completed
Pull Request — master (#1)
by Westin
06:59
created

MainConfig::validateConfigAndSetDefaults()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 12
nc 6
nop 1
crap 6
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 25
    public function __construct(array $config)
19
    {
20 25
        $this->validateConfigAndSetDefaults($config);
21 24
        $this->config = $config;
22 24
        $this->buildAdaptorConfigs();
23 24
        $this->buildFileSystemConfigs();
24 24
        $this->buildCacheConfigs();
25 24
    }
26
27 25
    public function validateConfigAndSetDefaults(&$config)
28
    {
29 25
        if (empty($config)
30 25
            || empty($config['flysystem'])
31
        ) {
32 3
            throw new MissingConfigException(
33 3
                'No config key of "flysystem" found in config array.'
34
            );
35
        }
36
37 24
        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 24
        if (empty($config['flysystem']['fileSystems']['default'])) {
44 1
            $config['flysystem']['fileSystems']['default'] = [];
45 1
        }
46
47
        if (empty($config['flysystem']['caches']['default'])) {
48 24
            $config['flysystem']['caches']['default']['type'] = 'memory';
49
        }
50
    }
51
52
    /**
53
     * @param $fileSystem
54 2
     * @return FileSystemConfig|null
55
     */
56 2
    public function getFileSystemConfig($fileSystem)
57
    {
58
        return $this->fileSystems[$fileSystem] ?? null;
59
    }
60
61
    /**
62
     * @param $adaptor
63 2
     * @return AdaptorConfig|null
64
     */
65 2
    public function getAdaptorConfig($adaptor)
66
    {
67
        return $this->adaptors[$adaptor] ?? null;
68
    }
69
70
    /**
71
     * @param $cache
72
     *
73 3
     * @return CacheConfig|null
74
     */
75 3
    public function getCacheConfig($cache)
76
    {
77
        return $this->cache[$cache] ?? null;
78 2
    }
79
80 2
    public function hasFileSystemConfig($fileSystem) : bool
81
    {
82
        return key_exists($fileSystem, $this->fileSystems);
83 2
    }
84
85 2
    public function hasAdaptorConfig($adaptor) : bool
86
    {
87
        return key_exists($adaptor, $this->adaptors);
88 2
    }
89
90 2
    public function hasCacheConfig($cache) : bool
91
    {
92
        return key_exists($cache, $this->cache);
93 24
    }
94
95 24
    protected function buildAdaptorConfigs()
96 24
    {
97
        foreach ($this->config['flysystem']['adaptors'] as $name => $adaptor) {
98 24
            $this->adaptors[$name] = new AdaptorConfig($adaptor);
99
        }
100 24
    }
101
102 24
    protected function buildCacheConfigs()
103 1
    {
104
        if (empty($this->config['flysystem']['caches'])) {
105
            return;
106 24
        }
107 24
108
        foreach ($this->config['flysystem']['caches'] as $name => $cache) {
109 24
            $this->cache[$name] = new CacheConfig($cache);
110
        }
111 24
    }
112
113 24
    protected function buildFileSystemConfigs()
114 24
    {
115
        foreach ($this->config['flysystem']['fileSystems'] as $name => $fileSystem) {
116 24
            $this->fileSystems[$name] = new FileSystemConfig($fileSystem);
117
        }
118
    }
119
}
120