Completed
Pull Request — master (#154)
by Arnaud
04:40
created

ResourceLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 43
ccs 0
cts 26
cp 0
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A load() 0 32 6
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