Completed
Push — master ( 8034c6...a22340 )
by Nils
04:00
created

EnvAwareYaml::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace whm\Smoke\Yaml;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class EnvAwareYaml
8
{
9
    public static function parse($fileContent)
10
    {
11
        preg_match_all('^\${(.*)}^', $fileContent, $matches);
12
13
        foreach ($matches[1] as $varName) {
14
            if (!getenv($varName)) {
15
                throw new \RuntimeException("The mandatory env variable (" . $varName . ") from the config file was not set.");
16
            }
17
18
            $fileContent = str_replace('${' . $varName . '}', getenv($varName), $fileContent);
19
        }
20
        return Yaml::parse($fileContent);
21
    }
22
23
}