Passed
Branch v2 (aaaa8e)
by Brent
03:38
created

YamlVariable::parseRecursive()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace Stitcher\Variable;
4
5
use Stitcher\File;
6
use Symfony\Component\Yaml\Yaml;
7
8
class YamlVariable extends AbstractVariable
9
{
10
    private $parser;
11
    private $variableParser;
12
13
    public function __construct(string $unparsed, Yaml $parser, VariableParser $variableParser)
14
    {
15
        parent::__construct($unparsed);
16
17
        $this->parser = $parser;
18
        $this->variableParser = $variableParser;
19
    }
20
21
    public static function make(string $value, Yaml $parser, VariableParser $variableParser): YamlVariable
22
    {
23
        return new self($value, $parser, $variableParser);
24
    }
25
26
    public function parse(): AbstractVariable
27
    {
28
        $this->parsed = $this->parser->parse(File::read($this->unparsed));
29
30
        $this->parsed = $this->parseRecursive($this->parsed);
31
32
        return $this;
33
    }
34
35
    private function parseRecursive($unparsedValue)
36
    {
37
        $unparsedValue = $this->variableParser->getVariable($unparsedValue);
38
39
        if ($unparsedValue instanceof DefaultVariable) {
40
            $parsedValue = $unparsedValue->parsed();
41
42
            if (is_array($parsedValue)) {
43
                foreach ($parsedValue as &$property) {
44
                    $property = $this->parseRecursive($property);
45
                }
46
            }
47
        } else {
48
            $parsedValue = $unparsedValue->parsed();
49
        }
50
51
        return $parsedValue;
52
    }
53
}
54