Completed
Push — master ( e8bfce...313e31 )
by Arnaud
13s queued 11s
created

ResourceLoader::load()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 32
ccs 0
cts 26
cp 0
crap 42
rs 9.0444
1
<?php
2
3
namespace LAG\AdminBundle\Resource\Loader;
4
5
use Exception;
6
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Yaml\Yaml;
10
11
class ResourceLoader
12
{
13
    /**
14
     * Load admins configuration in the yaml files found in the given resource path. An exception will be thrown if the
15
     * path is invalid.
16
     *
17
     * @throws Exception
18
     */
19
    public function load(string $resourcesPath): array
20
    {
21
        $fileSystem = new Filesystem();
22
23
        if (!$fileSystem->exists($resourcesPath)) {
24
            throw new FileNotFoundException(null, 0, null, $resourcesPath);
25
        }
26
27
        if (!is_dir($resourcesPath)) {
28
            throw new Exception(sprintf('The resources path %s should be a directory', $resourcesPath));
29
        }
30
        $finder = new Finder();
31
        $finder
32
            ->files()
33
            ->name('*.yaml')
34
            ->in($resourcesPath)
35
        ;
36
        $data = [];
37
38
        foreach ($finder as $fileInfo) {
39
            $yaml = Yaml::parse(file_get_contents($fileInfo->getRealPath()));
40
41
            if (!is_array($yaml)) {
42
                continue;
43
            }
44
45
            foreach ($yaml as $name => $admin) {
46
                $data[$name] = $admin;
47
            }
48
        }
49
50
        return $data;
51
    }
52
}
53