Completed
Push — master ( b31514...603a9f )
by Changwan
06:16
created

PathLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A test() 0 4 1
C load() 0 23 9
1
<?php
2
namespace Wandu\Config\Loader;
3
4
use DirectoryIterator;
5
use Wandu\Config\Contracts\Loader;
6
7
class PathLoader implements Loader
8
{
9
    /** @var \Wandu\Config\Contracts\Loader[] */
10
    protected $loaders;
11
    
12
    /** @var string */
13
    protected $pattern;
14
    
15 2
    public function __construct(array $loaders = [], string $pattern = '~^[a-z_][a-z0-9_]*$~')
16
    {
17 2
        $this->loaders = $loaders;
18 2
        $this->pattern = $pattern;
19 2
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 2
    public function test(string $path): bool
25
    {
26 2
        return is_dir($path);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function load(string $path)
33
    {
34 2
        $configToReturn = [];
35 2
        foreach (new DirectoryIterator($path) as $file) {
36 2
            $filename = $file->getFilename();
37 2
            if ($filename === '.' || $filename === '..') continue;
38 2
            if ($file->isFile()) {
39 2
                $name = $file->getBasename("." . $file->getExtension());
40 2
                foreach ($this->loaders as $loader) {
41 2
                    if ($loader->test($file->getRealPath())) {
42 2
                        if ($config = $loader->load($file->getRealPath())) {
43 2
                            $configToReturn[$name] = $config;
44
                        }
45
                    }
46
                }
47
            } else {
48 1
                if (preg_match($this->pattern, $filename)) {
49 2
                    $configToReturn[$filename] = $this->load($file->getRealPath());
50
                }
51
            }
52
        }
53 2
        return $configToReturn;
54
    }
55
}
56