Completed
Push — nln-php7 ( 1a6b54...2856d9 )
by Nicolas
02:57
created

ProfileReader   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 2
dl 0
loc 196
ccs 82
cts 82
cp 1
rs 9.84
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma;
6
7
use Gaufrette\Filesystem;
8
use Symfony\Component\Yaml\Yaml;
9
use Symfony\Component\Yaml\Exception\ParseException;
10
11
class ProfileReader implements FormattersDefinition
12
{
13
    private const
14
        TEMPLATE_SUFFIX_INDEX = 'suffix',
15
        MASTER_FILENAME_INDEX = 'master',
16
        CONFIGURATION_DIRECTORY_INDEX = 'confDir',
17
        SOURCE_PATH_INDEX = 'sourcePath',
18
        TARGET_PATH_INDEX = 'targetPath',
19
        DEFAULT_FORMATTER_INDEX = 'defaultFormatter',
20
        FORMATTERS_INDEX = 'formatters',
21
        FILE_EXTENSION_FORMATTERS_INDEX = 'fileExtensionFormatters',
22
        GENERATOR_INDEX = 'generator';
23
24
    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...
25
        $attributes;
26
27 53
    public function __construct(Filesystem $fs)
28
    {
29 53
        $this->attributes = [
30 53
            self::TEMPLATE_SUFFIX_INDEX => null,
31 53
            self::MASTER_FILENAME_INDEX => null,
32 53
            self::CONFIGURATION_DIRECTORY_INDEX => null,
33 53
            self::SOURCE_PATH_INDEX => null,
34 53
            self::TARGET_PATH_INDEX => null,
35 53
            self::DEFAULT_FORMATTER_INDEX => self::DEFAULT_FORMATTER_NAME,
36 53
            self::FORMATTERS_INDEX => array(),
37 53
            self::FILE_EXTENSION_FORMATTERS_INDEX => array(),
38 53
            self::GENERATOR_INDEX => array(),
39
        ];
40
41 53
        $this->read($fs);
42 51
    }
43
44 53
    private function read(Filesystem $fs)
45
    {
46 53
        $profileFilename = Application::PROFILE_FILENAME;
47
48 53
        if($fs->has($profileFilename))
49
        {
50 26
            $this->processProfileContent($fs->read($profileFilename));
51
        }
52 51
    }
53
54 26
    private function processProfileContent($content): void
55
    {
56
        try
57
        {
58 26
            $values = Yaml::parse($content);
59
        }
60 1
        catch(ParseException $e)
61
        {
62 1
            throw new \RuntimeException(sprintf(
63 1
               'Error while parsing profile : %s',
64 1
                $e->getMessage()
65
            ));
66
        }
67
68 25
        foreach(array_keys($this->attributes) as $name)
69
        {
70 25
            if(! isset($values[$name]))
71
            {
72 25
                continue;
73
            }
74
75 25
            $this->ensureParameterFormatIsValid($name, $values[$name]);
76
77 24
            $this->attributes[$name] = $values[$name];
78
        }
79 24
    }
80
81 44
    public function hasTemplatesSuffix(): bool
82
    {
83 44
        return $this->hasString(self::TEMPLATE_SUFFIX_INDEX);
84
    }
85
86 8
    public function getTemplatesSuffix()
87
    {
88 8
        return $this->getString(self::TEMPLATE_SUFFIX_INDEX);
89
    }
90
91 44
    public function hasMasterFilename(): bool
92
    {
93 44
        return $this->hasString(self::MASTER_FILENAME_INDEX);
94
    }
95
96 8
    public function getMasterFilename()
97
    {
98 8
        return $this->getString(self::MASTER_FILENAME_INDEX);
99
    }
100
101 44
    public function hasConfigurationDirectory(): bool
102
    {
103 44
        return $this->hasString(self::CONFIGURATION_DIRECTORY_INDEX);
104
    }
105
106 8
    public function getConfigurationDirectory()
107
    {
108 8
        return $this->getString(self::CONFIGURATION_DIRECTORY_INDEX);
109
    }
110
111 10
    public function hasSourcePath(): bool
112
    {
113 10
        return $this->has(self::SOURCE_PATH_INDEX);
114
    }
115
116 6
    public function getSourcePath()
117
    {
118 6
        return $this->get(self::SOURCE_PATH_INDEX);
119
    }
120
121 26
    public function hasTargetPath(): bool
122
    {
123 26
        return $this->has(self::TARGET_PATH_INDEX);
124
    }
125
126 1
    public function getTargetPath()
127
    {
128 1
        return $this->get(self::TARGET_PATH_INDEX);
129
    }
130
131 16
    public function getDefaultFormatterName(): ?string
132
    {
133 16
        return $this->getString(self::DEFAULT_FORMATTER_INDEX);
134
    }
135
136 15
    public function getFormatters()
137
    {
138 15
        return $this->get(self::FORMATTERS_INDEX);
139
    }
140
141 14
    public function getFileExtensionFormatters()
142
    {
143 14
        return $this->get(self::FILE_EXTENSION_FORMATTERS_INDEX);
144
    }
145
146 8
    public function getGeneratorOptions()
147
    {
148 8
        return $this->get(self::GENERATOR_INDEX);
149
    }
150
151 39
    private function has(string $attributeName): bool
152
    {
153 39
        return isset($this->attributes[$attributeName]);
154
    }
155
156 49
    private function hasString(string $attributeName): bool
157
    {
158 49
        return isset($this->attributes[$attributeName]) && is_string($this->attributes[$attributeName]);
159
    }
160
161 24
    private function get(string $attributeName)
162
    {
163 24
        $value = null;
164
165 24
        if($this->has($attributeName))
166
        {
167 24
            $value = $this->attributes[$attributeName];
168
        }
169
170 24
        return $value;
171
    }
172
173 24
    private function getString(string $attributeName): ?string
174
    {
175 24
        $value = null;
176
177 24
        if($this->hasString($attributeName))
178
        {
179 19
            $value = $this->attributes[$attributeName];
180
        }
181
182 24
        return $value;
183
    }
184
185 25
    private function ensureParameterFormatIsValid($parameter, $value): void
186
    {
187
        $parameterValidators = [
188 25
            'targetPath' => function($value) {
189 2
                return is_string($value);
190 25
            }
191
        ];
192
193
        if(
194 25
            ! array_key_exists($parameter, $parameterValidators)
195 25
            || ! $parameterValidators[$parameter] instanceof \Closure
196
        )
197
        {
198 24
            return;
199
        }
200
201 2
        if(! $parameterValidators[$parameter]($value))
202
        {
203 1
            throw new \RuntimeException('Paramater %s format is invalid');
204
        }
205 1
    }
206
}
207