Completed
Push — master ( c5de01...c9c19d )
by
unknown
07:34 queued 05:10
created

YamlLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Coduo\TuTu\Config\Loader;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class YamlLoader implements Loader
8
{
9
    /**
10
     * @var string
11
     */
12
    private $responsesYamlPath;
13
14
    /**
15
     * @var string
16
     */
17
    private $responsesYamlDir;
18
19
    /**
20
     * @param $responsesYamlPath
21
     * @throws \InvalidArgumentException
22
     * @internal param $ [] $yamlFileContent
23
     */
24
    public function __construct($responsesYamlPath)
25
    {
26
        if (!file_exists($responsesYamlPath)) {
27
            throw new \InvalidArgumentException(sprintf("File \"%s\" does not exist.", $responsesYamlPath));
28
        }
29
30
        $this->responsesYamlPath = $responsesYamlPath;
31
        $this->responsesYamlDir = dirname($this->responsesYamlPath);
32
    }
33
34
    /**
35
     * @throws \RuntimeException
36
     * @return array
37
     */
38
    public function getResponsesArray()
39
    {
40
        $config = Yaml::parse(file_get_contents($this->responsesYamlPath));
41
        $config = is_array($config) ? $config : [];
42
43
        $includes = (array_key_exists('includes', $config)) ? $config['includes'] : [];
44
        if (count($includes)) {
45
            unset($config['includes']);
46
47
            foreach ($includes as $fileName) {
48
                $includeFilePath = $this->responsesYamlDir . '/' . $fileName;
49
                $loader = new YamlLoader($includeFilePath);
50
                $config = array_merge($config, $loader->getResponsesArray());
51
            }
52
        }
53
54
        return $config;
55
    }
56
}
57