Completed
Push — nln-php7 ( 6680df...1a6b54 )
by Nicolas
02:06
created

YamlGenerator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 79
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma\Generator\ConfigurationFileGenerators;
6
7
use Karma\Generator\ConfigurationFileGenerator;
8
use Gaufrette\Filesystem;
9
use Karma\Configuration;
10
use Karma\Generator\VariableProvider;
11
use Symfony\Component\Yaml\Yaml;
12
use Karma\Application;
13
14
class YamlGenerator extends AbstractFileGenerator implements ConfigurationFileGenerator
15
{
16
    private array
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
17
        $files;
18
19 14
    public function __construct(Filesystem $fs, Configuration $reader, VariableProvider $variableProvider)
20
    {
21 14
        parent::__construct($fs, $reader, $variableProvider);
22
23 14
        $this->files = [];
24 14
    }
25
26 13
    protected function generateVariable(string $variableName, $value): void
27
    {
28 13
        if(stripos($variableName, self::DELIMITER) === false)
29
        {
30 1
            throw new \RuntimeException(sprintf(
31 1
               'Variable %s does not contain delimiter (%s)',
32
                $variableName,
33 1
                self::DELIMITER
34
            ));
35
        }
36
37 13
        $parts = explode(self::DELIMITER, $variableName);
38 13
        $current = & $this->files;
39
40 13
        foreach($parts as $part)
41
        {
42 13
            if(! isset($current[$part]))
43
            {
44 13
                $current[$part] = array();
45
            }
46
47 13
            $current= & $current[$part];
48
        }
49
50 13
        $current = $value;
51 13
    }
52
53 12
    protected function postGenerate(): void
54
    {
55 12
        if($this->dryRun === true)
56
        {
57 1
            return;
58
        }
59
60 11
        foreach($this->files as $file => $content)
61
        {
62 11
            $filename = $this->computeFilename($file);
63
64 11
            $this->backupFile($filename);
65 11
            $this->fs->write($filename, $this->formatContent($content), true);
66
        }
67 11
    }
68
69 11
    private function backupFile(string $filename): void
70
    {
71 11
        if($this->enableBackup === true)
72
        {
73 2
            if($this->fs->has($filename))
74
            {
75 2
                $content = $this->fs->read($filename);
76 2
                $backupFilename = $filename . Application::BACKUP_SUFFIX;
77
78 2
                $this->fs->write($backupFilename, $content, true);
79
            }
80
        }
81 11
    }
82
83 11
    private function computeFilename(string $file): string
84
    {
85 11
        return "$file.yml";
86
    }
87
88 11
    private function formatContent($content): string
89
    {
90 11
        return Yaml::dump($content);
91
    }
92
}
93