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

YamlVariable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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