YamlLocator::locateCollection()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 29
rs 9.3554
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Metadata\Locator;
6
7
use CuyZ\Valinor\Mapper\Source\Source;
8
use CuyZ\Valinor\MapperBuilder;
9
use LAG\AdminBundle\Exception\Exception;
10
use LAG\AdminBundle\Metadata\AdminResource;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Yaml\Yaml;
14
15
class YamlLocator implements MetadataLocatorInterface
16
{
17
    public function locateCollection(string $resourceDirectory): iterable
18
    {
19
        $fileSystem = new Filesystem();
20
21
        if (!$fileSystem->exists($resourceDirectory) || !is_dir($resourceDirectory)) {
22
            throw new Exception(sprintf('The resources path %s does not exists or is not a directory', $resourceDirectory));
23
        }
24
        $finder = new Finder();
25
        $finder
26
            ->files()
27
            ->name('*.yaml')
28
            ->in($resourceDirectory)
29
        ;
30
        $resources = [];
31
32
        foreach ($finder as $fileInfo) {
33
            $yaml = Yaml::parse(file_get_contents($fileInfo->getRealPath()), Yaml::PARSE_CUSTOM_TAGS);
34
35
            foreach ($yaml as $name => $configuration) {
36
                $resource = (new MapperBuilder())
37
                    ->mapper()
38
                    ->map(AdminResource::class, Source::array($configuration ?? []))
39
                ;
40
                $resource = $resource->withName($name);
41
                $resources[] = $resource;
42
            }
43
        }
44
45
        return $resources;
46
    }
47
}
48