Completed
Push — 57-formatter-per-extension ( 73b3c8 )
by Nicolas
40:17 queued 36:16
created

ProfileReader::processProfileContent()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 32
rs 6.7273
cc 7
eloc 14
nc 13
nop 1
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
use Karma\Formatters\Raw;
9
use Karma\Formatters\Rules;
10
11
class ProfileReader implements FormatterProvider
12
{
13
    const
14
        DEFAULT_FORMATTER_NAME = 'default',
15
    
16
        TEMPLATE_SUFFIX_INDEX = 'suffix',
17
        MASTER_FILENAME_INDEX = 'master',
18
        CONFIGURATION_DIRECTORY_INDEX = 'confDir',
19
        SOURCE_PATH_INDEX = 'sourcePath',
20
        FORMATTERS_INDEX = 'formatters',
21
        FILE_EXTENSION_FORMATTERS_INDEX = 'fileExtensionFormatters',
22
        DEFAULT_FORMATTER_INDEX = 'defaultFormatter';
23
    
24
    private
25
        $attributes,
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...
26
        $formatters,
27
        $fileExtensionFormatters;
28
    
29
    public function __construct(Filesystem $fs)
30
    {
31
        $this->attributes = array(
32
            self::TEMPLATE_SUFFIX_INDEX => null,
33
            self::MASTER_FILENAME_INDEX => null,
34
            self::CONFIGURATION_DIRECTORY_INDEX => null,
35
            self::SOURCE_PATH_INDEX => null,
36
            self::DEFAULT_FORMATTER_INDEX => self::DEFAULT_FORMATTER_NAME    
37
        );
38
        
39
        $this->formatters = array(
40
            self::DEFAULT_FORMATTER_NAME => new Raw(),
41
        );
42
        
43
        $this->fileExtensionFormatters = array();
44
45
        $this->read($fs);
46
    }
47
    
48
    private function read(Filesystem $fs)
49
    {
50
        $profileFilename = Application::PROFILE_FILENAME; 
51
        
52
        if($fs->has($profileFilename))
53
        {
54
            $this->processProfileContent($fs->read($profileFilename));
55
        }    
56
    }
57
    
58
    private function processProfileContent($content)
59
    {
60
        try
61
        {
62
            $values = Yaml::parse($content);
63
        }
64
        catch(ParseException $e)
65
        {
66
            throw new \RuntimeException(sprintf(
67
               'Error while parsing profile : %s',
68
                $e->getMessage()
69
            ));            
70
        }
71
        
72
        foreach(array_keys($this->attributes) as $name)
73
        {
74
            if(isset($values[$name]) && is_string($values[$name]))
75
            {
76
                $this->attributes[$name] = $values[$name];
77
            }
78
        }
79
        
80
        if(isset($values[self::FORMATTERS_INDEX]))
81
        {
82
            $this->parseFormatters($values[self::FORMATTERS_INDEX]);    
83
        }
84
        
85
        if(isset($values[self::FILE_EXTENSION_FORMATTERS_INDEX]))
86
        {
87
            $this->fileExtensionFormatters = array_map('trim', $values[self::FILE_EXTENSION_FORMATTERS_INDEX]);    
88
        }
89
    }
90
    
91
    private function parseFormatters($content)
92
    {
93
        if(! is_array($content))
94
        {
95
            throw new \InvalidArgumentException('Syntax error in profile [formatters]');
96
        }
97
        
98
        foreach($content as $name => $rules)
99
        {
100
            if(! is_array($rules))
101
            {
102
                throw new \InvalidArgumentException('Syntax error in profile [formatters]');
103
            }
104
            
105
            $this->formatters[$name] = new Rules($rules);
106
        }
107
    }
108
    
109
    public function hasTemplatesSuffix()
110
    {
111
        return $this->has(self::TEMPLATE_SUFFIX_INDEX);
112
    }
113
    
114
    public function getTemplatesSuffix()
115
    {
116
        return $this->get(self::TEMPLATE_SUFFIX_INDEX);
117
    }
118
    
119
    public function hasMasterFilename()
120
    {
121
        return $this->has(self::MASTER_FILENAME_INDEX);
122
    }
123
    
124
    public function getMasterFilename()
125
    {
126
        return $this->get(self::MASTER_FILENAME_INDEX);
127
    }
128
    
129
    public function hasConfigurationDirectory()
130
    {
131
        return $this->has(self::CONFIGURATION_DIRECTORY_INDEX);
132
    }
133
    
134
    public function getConfigurationDirectory()
135
    {
136
        return $this->get(self::CONFIGURATION_DIRECTORY_INDEX);
137
    }
138
    
139
    public function hasSourcePath()
140
    {
141
        return $this->has(self::SOURCE_PATH_INDEX);
142
    }
143
    
144
    public function getSourcePath()
145
    {
146
        return $this->get(self::SOURCE_PATH_INDEX);
147
    }
148
    
149
    private function has($attributeName)
150
    {
151
        return isset($this->attributes[$attributeName]);
152
    }
153
    
154
    private function get($attributeName)
155
    {
156
        $value = null;
157
158
        if($this->has($attributeName))
159
        {
160
            $value = $this->attributes[$attributeName];
161
        }
162
        
163
        return $value;
164
    }
165
    
166
    public function hasFormatter($index)
167
    {
168
        return isset($this->formatters[$index]);
169
    }
170
    
171
    public function getFormatter($fileExtension, $index = null)
172
    {
173
        $formatter = $this->formatters[$this->getDefaultFormatterName()];
174
        
175
        if($this->hasFormatter($index))
176
        {
177
            $formatter = $this->formatters[$index];    
178
        }    
179
        
180
        return $formatter;
181
    }
182
    
183
    private function getDefaultFormatterName()
184
    {
185
        $name = self::DEFAULT_FORMATTER_NAME;
186
        
187
        $defaultFormatterName = $this->get(self::DEFAULT_FORMATTER_INDEX);
188
        
189
        if($this->hasFormatter($defaultFormatterName))
190
        {
191
            $name = $defaultFormatterName;
192
        }
193
        
194
        return $name;
195
    }
196
}