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

AbstractFileGenerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 78
ccs 31
cts 33
cp 0.9394
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\ConfigurableProcessor;
8
use Karma\Generator\ConfigurationFileGenerator;
9
use Gaufrette\Filesystem;
10
use Karma\Configuration;
11
use Karma\Generator\VariableProvider;
12
13
abstract class AbstractFileGenerator implements ConfigurationFileGenerator
14
{
15
    protected Filesystem
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_STRING, expecting T_FUNCTION or T_CONST
Loading history...
16
        $fs;
17
    protected Configuration
18
        $reader;
19
    protected VariableProvider
20
        $variableProvider;
21
    protected bool
22
        $dryRun,
23
        $enableBackup;
24
    protected ?string
25
        $systemEnvironment;
26
27 14
    public function __construct(Filesystem $fs, Configuration $reader, VariableProvider $variableProvider)
28
    {
29 14
        $this->fs = $fs;
30 14
        $this->reader = $reader;
31 14
        $this->variableProvider = $variableProvider;
32 14
        $this->dryRun = false;
33 14
        $this->enableBackup = false;
34 14
        $this->systemEnvironment = null;
35 14
    }
36
37 1
    public function setDryRun(bool $value = true): ConfigurableProcessor
38
    {
39 1
        $this->dryRun = (bool) $value;
40
41 1
        return $this;
42
    }
43
44 3
    public function enableBackup(bool $value = true): ConfigurableProcessor
45
    {
46 3
        $this->enableBackup = (bool) $value;
47
48 3
        return $this;
49
    }
50
51 13
    public function generate(string $environment): void
52
    {
53 13
        $this->preGenerate();
54
55 13
        $variables = $this->variableProvider->getAllVariables();
56
57 13
        foreach($variables as $variable => $translatedVariableName)
58
        {
59 13
            $value = $this->read($variable, $environment);
60 13
            $this->generateVariable($translatedVariableName, $value);
61
        }
62
63 12
        $this->postGenerate();
64 12
    }
65
66 13
    private function read($variable, $environment)
67
    {
68 13
        if($this->systemEnvironment !== null && $this->reader->isSystem($variable))
69
        {
70 1
            $environment = $this->systemEnvironment;
71
        }
72
73 13
        return $this->reader->read($variable, $environment);
74
    }
75
76 2
    public function setSystemEnvironment(?string $environment): ConfigurableProcessor
77
    {
78 2
        $this->systemEnvironment = $environment;
79
80 2
        return $this;
81
    }
82
83
    abstract protected function generateVariable(string $variableName, $value): void;
84
85 13
    protected function preGenerate(): void
86
    {
87
88 13
    }
89
90
    protected function postGenerate(): void
91
    {
92
93
    }
94
}
95