Completed
Pull Request — master (#154)
by Arnaud
04:40
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
     * @param string $resourcesPath
18
     *
19
     * @return array
20
     * @throws Exception
21
     */
22
    public function load(string $resourcesPath): array
23
    {
24
        $fileSystem = new Filesystem();
25
26
        if (!$fileSystem->exists($resourcesPath)) {
27
            throw new FileNotFoundException(null, 0, null, $resourcesPath);
28
        }
29
30
        if (!is_dir($resourcesPath)) {
31
            throw new Exception(sprintf('The resources path %s should be a directory', $resourcesPath));
32
        }
33
        $finder = new Finder();
34
        $finder
35
            ->files()
36
            ->name('*.yaml')
37
            ->in($resourcesPath)
38
        ;
39
        $data = [];
40
41
        foreach ($finder as $fileInfo) {
42
            $yaml = Yaml::parse(file_get_contents($fileInfo->getRealPath()));
43
44
            if (!is_array($yaml)) {
45
                continue;
46
            }
47
48
            foreach ($yaml as $name => $admin) {
49
                $data[$name] = $admin;
50
            }
51
        }
52
53
        return $data;
54
    }
55
}
56