Completed
Branch master (1f9106)
by Nils
02:44
created

EnvAwareYaml::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
rs 9.4285
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
        foreach ($matches[1] as $varName) {
13
            if (!getenv($varName)) {
14
                throw new \RuntimeException('The mandatory env variable (' . $varName . ') from the config file was not set.');
15
            }
16
17
            $fileContent = str_replace('${' . $varName . '}', getenv($varName), $fileContent);
18
        }
19
20
        return Yaml::parse($fileContent);
21
    }
22
}
23