Passed
Push — develop ( ec1e42...80559e )
by Brent
03:22
created

YamlVariable::parseRecursive()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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