YamlRecursivePathsFinder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 69
ccs 23
cts 24
cp 0.9583
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findPathRecursively() 0 17 5
A getImports() 0 9 2
A __construct() 0 6 2
A getAllPaths() 0 5 1
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 1
    public function __construct(array $paths)
22
    {
23 1
        $this->paths = [];
24
25 1
        if (false !== $paths = array_combine($paths, $paths)) {
26 1
            $this->paths = $paths;
27
        }
28 1
    }
29
30
    /**
31
     * @return array
32
     */
33 1
    public function getAllPaths()
34
    {
35 1
        $this->findPathRecursively($this->paths);
36
37 1
        return $this->paths;
38
    }
39
40
    /**
41
     * @param array $paths
42
     */
43 1
    private function findPathRecursively(array $paths): void
44
    {
45 1
        foreach ($paths as $path) {
46 1
            if (!file_exists($path)) {
47 1
                continue;
48
            }
49
50 1
            $this->paths[$path] = $path;
51
52 1
            foreach ($this->getImports($path) as $import) {
53 1
                $resource = $import['resource'];
54
55 1
                if (isset($this->paths[$resource])) {
56 1
                    continue;
57
                }
58
59 1
                $this->findPathRecursively([$resource]);
60
            }
61
        }
62 1
    }
63
64
    /**
65
     * @param string $path
66
     *
67
     * @return mixed
68
     */
69 1
    private function getImports($path)
70
    {
71 1
        if (null === $yaml = Yaml::parseFile($path)) {
72
            $yaml = [];
73
        }
74
75 1
        $yaml += ['imports' => []];
76
77 1
        return $yaml['imports'];
78
    }
79
}
80