Completed
Push — master ( ca2abf...933c4d )
by Westin
05:20
created

src/Config/FileSystemConfig.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem\Config;
5
6
use WShafer\PSR11FlySystem\Exception\MissingConfigException;
7
8
class FileSystemConfig
9
{
10
    /** @var array  */
11
    protected $fileSystems = [];
12
13
    /** @var array  */
14
    protected $config = [];
15
16
    /**
17
     * FileSystemConfig constructor.
18
     *
19
     * @param array $config
20
     */
21 12
    public function __construct(array $config)
22
    {
23 12
        $this->validateConfig($config);
24 11
        $this->config = $config;
25
26 11
        if ($this->isManager()) {
27 3
            $this->buildManagerFileSystemConfigs();
28
        }
29 10
    }
30
31
    /**
32
     * Validate the config array
33
     *
34
     * @param $config
35
     */
36 12 View Code Duplication
    public function validateConfig($config)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38 12
        if (empty($config)) {
39 1
            throw new MissingConfigException(
40 1
                'No config found'
41
            );
42
        }
43
44 11
        if (empty($config['adaptor'])) {
45 1
            throw new MissingConfigException(
46 1
                'No config key of "type" found in adaptor config array.'
47
            );
48
        }
49 11
    }
50
51
    /**
52
     * Get the adaptor
53
     *
54
     * @return string
55
     */
56 11
    public function getAdaptor()
57
    {
58 11
        return $this->config['adaptor'];
59
    }
60
61
    /**
62
     * Get the cache
63
     *
64
     * @return string
65
     */
66 1
    public function getCache()
67
    {
68 1
        return $this->config['cache'] ?? 'default';
69
    }
70
71
    /**
72
     * Get the plugins
73
     *
74
     * @return array
75
     */
76 1
    public function getPlugins()
77
    {
78 1
        return $this->config['plugins'] ?? [];
79
    }
80
81
    /**
82
     * Is this defined as a file manager?
83
     *
84
     * @return bool
85
     */
86 11
    public function isManager()
87
    {
88 11
        if ($this->getAdaptor() == 'manager') {
89 3
            return true;
90
        }
91
92 10
        return false;
93
    }
94
95
    /**
96
     * Get the configured File Systems.
97
     * Used for manager configuration
98
     *
99
     * @return FileSystemConfig[]
100
     */
101 1
    public function getFileSystems()
102
    {
103 1
        return $this->fileSystems;
104
    }
105
106
    /**
107
     * Build out the file system configs for use
108
     * by the manager.
109
     */
110 3
    protected function buildManagerFileSystemConfigs()
111
    {
112 3
        if (empty($this->config['fileSystems'])) {
113 1
            throw new MissingConfigException(
114 1
                'Missing file systems for manager'
115
            );
116
        }
117
118 2
        foreach ($this->config['fileSystems'] as $name => $fileSystem) {
119 2
            $this->fileSystems[$name] = new self($fileSystem);
120
        }
121 2
    }
122
}
123