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

YamlVariable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A make() 0 4 1
A parse() 0 8 1
A parseRecursive() 0 18 4
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