Completed
Push — master ( d412ce...8871ca )
by Fumio
03:00
created

ConfigLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 8 1
A loadConfigurationFiles() 0 6 2
A getConfigurationFiles() 0 13 3
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel\Repository;
4
5
use Illuminate\Config\Repository;
6
use Symfony\Component\Finder\Finder;
7
8
class ConfigLoader
9
{
10
    /**
11
     * Load the configuration items from all of the files.
12
     *
13
     * @param string $directoryPath
14
     *
15
     * @return Illuminate\Contracts\Config\Repository
16
     */
17 8
    public static function load($directoryPath)
18
    {
19 8
        $config = new Repository();
20
21 8
        (new static())->loadConfigurationFiles($directoryPath, $config);
22
23 8
        return $config;
24
    }
25
26
    /**
27
     * Load the configuration items from all of the files.
28
     *
29
     * @param string                                 $directoryPath
30
     * @param \Illuminate\Contracts\Config\Repository $config
31
     */
32 8
    protected function loadConfigurationFiles($directoryPath, Repository $config)
33
    {
34 8
        foreach ($this->getConfigurationFiles($directoryPath) as $group => $path) {
35
            $config->set($group, require $path);
36 8
        }
37 8
    }
38
39
    /**
40
     * Get all of the configuration files for the directory.
41
     *
42
     * @param string $directoryPath
43
     *
44
     * @return array
45
     */
46 8
    protected function getConfigurationFiles($directoryPath)
47
    {
48 8
        $files = [];
49
50 8
        if (is_dir($directoryPath)) {
51 2
            foreach (Finder::create()->files()->in($directoryPath) as $file) {
52
                $group = basename($file->getRealPath(), '.php');
53
                $files[$group] = $file->getRealPath();
54 2
            }
55 2
        }
56
57 8
        return $files;
58
    }
59
}
60