Configuration::parseFiles()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
rs 8.9137
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 6.0131
1
<?php
2
3
namespace PhpAbac\Configuration;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\Config\Loader\LoaderInterface;
7
8
use PhpAbac\Loader\JsonLoader;
9
use PhpAbac\Loader\YamlLoader;
10
11
class Configuration implements ConfigurationInterface
12
{
13
    /** @var AbacLoader[] * */
14
    protected $loaders = [];
15
    /** @var array * */
16
    protected $rules = [];
17
    /** @var array * */
18
    protected $attributes = [];
19
    /** @var array List of File Already Loaded */
20
    protected $loadedFiles = [];
21
    
22
    const LOADERS = [
23
        JsonLoader::class,
24
        YamlLoader::class
25
    ];
26
    
27 5
    public function __construct(array $configurationFiles, string $configDir = null)
28
    {
29 5
        $this->initLoaders($configDir);
30 5
        $this->parseFiles($configurationFiles);
31 5
    }
32
    
33 5
    protected function initLoaders(string $configDir = null)
34
    {
35 5
        $locator = new FileLocator($configDir);
36 5
        foreach (self::LOADERS as $loaderClass) {
37 5
            $loader = new $loaderClass($locator);
38 5
            $loader->setCurrentDir($configDir);
39 5
            $this->loaders[] = $loader;
40
        }
41 5
    }
42
    
43 5
    protected function parseFiles(array $configurationFiles)
44
    {
45 5
        foreach ($configurationFiles as $configurationFile) {
46 5
            $config = $this->getLoader($configurationFile)->import($configurationFile, pathinfo($configurationFile, PATHINFO_EXTENSION));
47
            
48 5
            if (in_array($configurationFile, $this->loadedFiles)) {
49
                continue;
50
            }
51
            
52 5
            $this->loadedFiles[] = $configurationFile;
53
            
54 5
            if (isset($config['@import'])) {
55 4
                $this->parseFiles($config['@import']);
56 4
                unset($config['@import']);
57
            }
58
            
59 5
            if (isset($config['attributes'])) {
60 5
                $this->attributes = array_merge($this->attributes, $config['attributes']);
61
            }
62 5
            if (isset($config['rules'])) {
63 5
                $this->rules = array_merge($this->rules, $config['rules']);
64
            }
65
        }
66 5
    }
67
    
68 5
    protected function getLoader(string $configurationFile): LoaderInterface
69
    {
70 5
        foreach ($this->loaders as $abacLoader) {
71 5
            if ($abacLoader->supports($configurationFile)) {
72 5
                return $abacLoader;
73
            }
74
        }
75
        throw new \Exception('Loader not found for the file ' . $configurationFile);
76
    }
77
    
78 5
    public function getAttributes(): array
79
    {
80 5
        return $this->attributes;
81
    }
82
    
83 5
    public function getRules(): array
84
    {
85 5
        return $this->rules;
86
    }
87
}
88