Completed
Push — master ( d24520...9dfa2d )
by Nicolas
05:12
created

ProfileReader::getTargetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Karma;
4
5
use Gaufrette\Filesystem;
6
use Symfony\Component\Yaml\Yaml;
7
use Symfony\Component\Yaml\Exception\ParseException;
8
9
class ProfileReader implements FormattersDefinition
10
{
11
    const
12
        TEMPLATE_SUFFIX_INDEX = 'suffix',
13
        MASTER_FILENAME_INDEX = 'master',
14
        CONFIGURATION_DIRECTORY_INDEX = 'confDir',
15
        SOURCE_PATH_INDEX = 'sourcePath',
16
        TARGET_PATH_INDEX = 'targetPath',
17
        DEFAULT_FORMATTER_INDEX = 'defaultFormatter',
18
        FORMATTERS_INDEX = 'formatters',
19
        FILE_EXTENSION_FORMATTERS_INDEX = 'fileExtensionFormatters',
20
        GENERATOR_INDEX = 'generator';
21
22
    private
23
        $attributes;
24
25
    public function __construct(Filesystem $fs)
26
    {
27
        $this->attributes = array(
28
            self::TEMPLATE_SUFFIX_INDEX => null,
29
            self::MASTER_FILENAME_INDEX => null,
30
            self::CONFIGURATION_DIRECTORY_INDEX => null,
31
            self::SOURCE_PATH_INDEX => null,
32
            self::TARGET_PATH_INDEX => null,
33
            self::DEFAULT_FORMATTER_INDEX => self::DEFAULT_FORMATTER_NAME,
34
            self::FORMATTERS_INDEX => array(),
35
            self::FILE_EXTENSION_FORMATTERS_INDEX => array(),
36
            self::GENERATOR_INDEX => array(),
37
        );
38
39
        $this->read($fs);
40
    }
41
42
    private function read(Filesystem $fs)
43
    {
44
        $profileFilename = Application::PROFILE_FILENAME;
45
46
        if($fs->has($profileFilename))
47
        {
48
            $this->processProfileContent($fs->read($profileFilename));
49
        }
50
    }
51
52
    private function processProfileContent($content)
53
    {
54
        try
55
        {
56
            $values = Yaml::parse($content);
57
        }
58
        catch(ParseException $e)
59
        {
60
            throw new \RuntimeException(sprintf(
61
               'Error while parsing profile : %s',
62
                $e->getMessage()
63
            ));
64
        }
65
66
        foreach(array_keys($this->attributes) as $name)
67
        {
68
            if(! isset($values[$name]))
69
            {
70
                continue;
71
            }
72
73
            $this->ensureParameterFormatIsValid($name, $values[$name]);
74
75
            $this->attributes[$name] = $values[$name];
76
        }
77
    }
78
79
    public function hasTemplatesSuffix()
80
    {
81
        return $this->hasString(self::TEMPLATE_SUFFIX_INDEX);
82
    }
83
84
    public function getTemplatesSuffix()
85
    {
86
        return $this->getString(self::TEMPLATE_SUFFIX_INDEX);
87
    }
88
89
    public function hasMasterFilename()
90
    {
91
        return $this->hasString(self::MASTER_FILENAME_INDEX);
92
    }
93
94
    public function getMasterFilename()
95
    {
96
        return $this->getString(self::MASTER_FILENAME_INDEX);
97
    }
98
99
    public function hasConfigurationDirectory()
100
    {
101
        return $this->hasString(self::CONFIGURATION_DIRECTORY_INDEX);
102
    }
103
104
    public function getConfigurationDirectory()
105
    {
106
        return $this->getString(self::CONFIGURATION_DIRECTORY_INDEX);
107
    }
108
109
    public function hasSourcePath()
110
    {
111
        return $this->has(self::SOURCE_PATH_INDEX);
112
    }
113
114
    public function getSourcePath()
115
    {
116
        return $this->get(self::SOURCE_PATH_INDEX);
117
    }
118
119
    public function hasTargetPath()
120
    {
121
        return $this->has(self::TARGET_PATH_INDEX);
122
    }
123
124
    public function getTargetPath()
125
    {
126
        return $this->get(self::TARGET_PATH_INDEX);
127
    }
128
129
    public function getDefaultFormatterName()
130
    {
131
        return $this->getString(self::DEFAULT_FORMATTER_INDEX);
132
    }
133
134
    public function getFormatters()
135
    {
136
        return $this->get(self::FORMATTERS_INDEX);
137
    }
138
139
    public function getFileExtensionFormatters()
140
    {
141
        return $this->get(self::FILE_EXTENSION_FORMATTERS_INDEX);
142
    }
143
144
    public function getGeneratorOptions()
145
    {
146
        return $this->get(self::GENERATOR_INDEX);
147
    }
148
149
    private function has($attributeName)
150
    {
151
        return isset($this->attributes[$attributeName]);
152
    }
153
154
    private function hasString($attributeName)
155
    {
156
        return isset($this->attributes[$attributeName]) && is_string($this->attributes[$attributeName]);
157
    }
158
159 View Code Duplication
    private function get($attributeName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $value = null;
162
163
        if($this->has($attributeName))
164
        {
165
            $value = $this->attributes[$attributeName];
166
        }
167
168
        return $value;
169
    }
170
171 View Code Duplication
    private function getString($attributeName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        $value = null;
174
175
        if($this->hasString($attributeName))
176
        {
177
            $value = $this->attributes[$attributeName];
178
        }
179
180
        return $value;
181
    }
182
183
    private function ensureParameterFormatIsValid($parameter, $value)
184
    {
185
        $parameterValidators = array(
186
            'targetPath' => function($value) {
187
                return is_string($value);
188
            }
189
        );
190
191
        if(
192
            ! array_key_exists($parameter, $parameterValidators)
193
            || ! $parameterValidators[$parameter] instanceof \Closure
194
        )
195
        {
196
            return true;
197
        }
198
199
        if(! $parameterValidators[$parameter]($value))
200
        {
201
            throw new \RuntimeException('Paramater %s format is invalid');
202
        }
203
    }
204
}
205