Completed
Pull Request — master (#96)
by Sébastien
03:27
created

ProfileReader::getGeneratorOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
                $this->attributes[$name] = $values[$name];
71
            }
72
        }
73
    }
74
75
    public function hasTemplatesSuffix()
76
    {
77
        return $this->hasString(self::TEMPLATE_SUFFIX_INDEX);
78
    }
79
80
    public function getTemplatesSuffix()
81
    {
82
        return $this->getString(self::TEMPLATE_SUFFIX_INDEX);
83
    }
84
85
    public function hasMasterFilename()
86
    {
87
        return $this->hasString(self::MASTER_FILENAME_INDEX);
88
    }
89
90
    public function getMasterFilename()
91
    {
92
        return $this->getString(self::MASTER_FILENAME_INDEX);
93
    }
94
95
    public function hasConfigurationDirectory()
96
    {
97
        return $this->hasString(self::CONFIGURATION_DIRECTORY_INDEX);
98
    }
99
100
    public function getConfigurationDirectory()
101
    {
102
        return $this->getString(self::CONFIGURATION_DIRECTORY_INDEX);
103
    }
104
105
    public function hasSourcePath()
106
    {
107
        return $this->has(self::SOURCE_PATH_INDEX);
108
    }
109
110
    public function getSourcePath()
111
    {
112
        return $this->get(self::SOURCE_PATH_INDEX);
113
    }
114
115
    public function hasTargetPath()
116
    {
117
        return $this->has(self::TARGET_PATH_INDEX);
118
    }
119
120
    public function getTargetPath()
121
    {
122
        return $this->get(self::TARGET_PATH_INDEX);
123
    }
124
125
    public function getDefaultFormatterName()
126
    {
127
        return $this->getString(self::DEFAULT_FORMATTER_INDEX);
128
    }
129
130
    public function getFormatters()
131
    {
132
        return $this->get(self::FORMATTERS_INDEX);
133
    }
134
135
    public function getFileExtensionFormatters()
136
    {
137
        return $this->get(self::FILE_EXTENSION_FORMATTERS_INDEX);
138
    }
139
140
    public function getGeneratorOptions()
141
    {
142
        return $this->get(self::GENERATOR_INDEX);
143
    }
144
145
    private function has($attributeName)
146
    {
147
        return isset($this->attributes[$attributeName]);
148
    }
149
150
    private function hasString($attributeName)
151
    {
152
        return isset($this->attributes[$attributeName]) && is_string($this->attributes[$attributeName]);
153
    }
154
155 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...
156
    {
157
        $value = null;
158
159
        if($this->has($attributeName))
160
        {
161
            $value = $this->attributes[$attributeName];
162
        }
163
164
        return $value;
165
    }
166
167 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...
168
    {
169
        $value = null;
170
171
        if($this->hasString($attributeName))
172
        {
173
            $value = $this->attributes[$attributeName];
174
        }
175
176
        return $value;
177
    }
178
}
179