Passed
Push — nln-php7 ( 12eaac )
by Nicolas
05:28
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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setDryRun() 0 6 1
A enableBackup() 0 6 1
A generate() 0 14 2
A read() 0 9 3
A setSystemEnvironment() 0 6 1
generateVariable() 0 1 ?
A preGenerate() 0 4 1
A postGenerate() 0 4 1
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
16
        $fs,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
17
        $reader,
18
        $variableProvider,
19
        $dryRun,
20
        $enableBackup,
21
        $systemEnvironment;
22
23 14
    public function __construct(Filesystem $fs, Configuration $reader, VariableProvider $variableProvider)
24
    {
25 14
        $this->fs = $fs;
26 14
        $this->reader = $reader;
27 14
        $this->variableProvider = $variableProvider;
28 14
        $this->dryRun = false;
29 14
        $this->enableBackup = false;
30 14
        $this->systemEnvironment = null;
31 14
    }
32
33 1
    public function setDryRun($value = true)
34
    {
35 1
        $this->dryRun = (bool) $value;
36
37 1
        return $this;
38
    }
39
40 3
    public function enableBackup($value = true)
41
    {
42 3
        $this->enableBackup = (bool) $value;
43
44 3
        return $this;
45
    }
46
47 13
    public function generate(string $environment): void
48
    {
49 13
        $this->preGenerate();
50
51 13
        $variables = $this->variableProvider->getAllVariables();
52
53 13
        foreach($variables as $variable => $translatedVariableName)
54
        {
55 13
            $value = $this->read($variable, $environment);
56 13
            $this->generateVariable($translatedVariableName, $value);
57
        }
58
59 12
        $this->postGenerate();
60 12
    }
61
62 13
    private function read($variable, $environment)
63
    {
64 13
        if($this->systemEnvironment !== null && $this->reader->isSystem($variable))
65
        {
66 1
            $environment = $this->systemEnvironment;
67
        }
68
69 13
        return $this->reader->read($variable, $environment);
70
    }
71
72 2
    public function setSystemEnvironment(string $environment): ConfigurableProcessor
73
    {
74 2
        $this->systemEnvironment = $environment;
75
76 2
        return $this;
77
    }
78
79
    abstract protected function generateVariable($variableName, $value);
80
81 13
    protected function preGenerate()
82
    {
83
84 13
    }
85
86
    protected function postGenerate()
87
    {
88
89
    }
90
}
91