Completed
Push — master ( c54e22...e7a101 )
by Bart
03:24
created

Data   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 70
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceEnvVariables() 0 22 4
A toYaml() 0 4 1
A parseYamlFile() 0 12 3
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Models;
4
5
use Exception;
6
use craft\base\Model;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Schematic Data Model.
11
 *
12
 * Encapsulates data that has been exported via schematic.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2019, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class Data extends Model
21
{
22
    /**
23
     * Replace placeholders with enviroment variables.
24
     *
25
     * Placeholders start with % and end with %. This will be replaced by the
26
     * environment variable with the name SCHEMATIC_{PLACEHOLDER}. If the
27
     * environment variable is not set an exception will be thrown.
28
     *
29
     * @param string $yaml
30
     *
31
     * @return string
32
     *
33
     * @throws Exception
34
     */
35 9
    public static function replaceEnvVariables($yaml): string
36
    {
37 9
        $matches = null;
38 9
        preg_match_all('/%\w+%/', $yaml, $matches);
39 9
        $originalValues = $matches[0];
40 9
        $replaceValues = [];
41 9
        foreach ($originalValues as $match) {
42 9
            $envVariable = substr($match, 1, -1);
43 9
            $envValue = getenv($envVariable);
44 9
            if (!$envValue) {
45 9
                $envVariable = strtoupper($envVariable);
46 9
                $envVariable = 'SCHEMATIC_'.$envVariable;
47 9
                $envValue = getenv($envVariable);
48 9
                if (!$envValue) {
49 1
                    throw new Exception("Schematic environment variable not set: {$envVariable}");
50
                }
51
            }
52 8
            $replaceValues[] = $envValue;
53
        }
54
55 8
        return str_replace($originalValues, $replaceValues, $yaml);
56
    }
57
58
    /**
59
     * Convert array to yaml.
60
     *
61
     * @param array  $data
62
     *
63
     * @return string
64
     */
65 3
    public static function toYaml(array $data): string
66
    {
67 3
        return Yaml::dump($data, 12, 2);
68
    }
69
70
    /**
71
     * Read yaml file and parse content.
72
     *
73
     * @param $file
74
     * @return array
75
     * @throws Exception
76
     */
77 9
    public static function parseYamlFile($file): array
78
    {
79 9
        if (file_exists($file)) {
80 9
            $data = file_get_contents($file);
81 9
            if (!empty($data)) {
82 9
                $data = static::replaceEnvVariables($data);
83 8
                return Yaml::parse($data);
84
            }
85
        }
86
87
        return [];
88
    }
89
}
90