Passed
Push — master ( 9e0b2c...e0b23c )
by Pol
03:34
created

YamlRecursivePathsFinder::getAllPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Config;
6
7
use Symfony\Component\Yaml\Yaml;
8
9
final class YamlRecursivePathsFinder
10
{
11
    /**
12
     * @var array
13
     */
14
    private $paths;
15
16
    /**
17
     * YamlRecursivePathsFinder constructor.
18
     *
19
     * @param array $paths
20
     */
21
    public function __construct(array $paths)
22
    {
23
        $this->paths = [];
24
25
        if (false !== $paths = \array_combine($paths, $paths)) {
26
            $this->paths = $paths;
27
        }
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public function getAllPaths()
34
    {
35
        $this->findPathRecursively($this->paths);
36
37
        return $this->paths;
38
    }
39
40
    /**
41
     * @param array $paths
42
     */
43
    private function findPathRecursively(array $paths)
44
    {
45
        foreach ($paths as $path) {
46
            if (!\file_exists($path)) {
47
                continue;
48
            }
49
50
            foreach ($this->getImports($path) as $import) {
51
                $resource = $import['resource'];
52
53
                if (isset($this->paths[$resource])) {
54
                    continue;
55
                }
56
57
                $this->paths[$resource] = $resource;
58
59
                $this->findPathRecursively([$resource]);
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param string $path
66
     *
67
     * @return mixed
68
     */
69
    private function getImports($path)
70
    {
71
        if (null === $yaml = Yaml::parseFile($path)) {
72
            $yaml = [];
73
        }
74
75
        $yaml += ['imports' => []];
76
77
        return $yaml['imports'];
78
    }
79
}
80