Total Complexity | 57 |
Total Lines | 387 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GlobalConfig often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GlobalConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class GlobalConfig extends BaseConfig { |
||
19 | |||
20 | /** Cache reference */ |
||
21 | private $cache; |
||
22 | /** Location of the configuration file. Used for caching. */ |
||
23 | private $filePath; |
||
24 | /** Namespaces from vocabularies configuration file. */ |
||
25 | private $namespaces; |
||
26 | /** EasyRdf\Graph graph */ |
||
27 | private $graph; |
||
28 | /** |
||
29 | * @var int the time the config file was last modified |
||
30 | */ |
||
31 | private $configModifiedTime = null; |
||
32 | |||
33 | public function __construct($config_name='/../config.ttl') |
||
34 | { |
||
35 | $this->cache = new Cache(); |
||
36 | try { |
||
37 | $this->filePath = realpath( dirname(__FILE__) . $config_name ); |
||
38 | if (!file_exists($this->filePath)) { |
||
39 | throw new Exception('config.ttl file is missing, please provide one.'); |
||
40 | } |
||
41 | $this->initializeConfig(); |
||
42 | } catch (Exception $e) { |
||
43 | echo "Error: " . $e->getMessage(); |
||
44 | return; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | public function getCache() |
||
49 | { |
||
50 | return $this->cache; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return int the time the config file was last modified |
||
55 | */ |
||
56 | public function getConfigModifiedTime() |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Initialize configuration, reading the configuration file from the disk, |
||
63 | * and creating the graph and resources objects. Uses a cache if available, |
||
64 | * in order to avoid re-loading the complete configuration on each request. |
||
65 | */ |
||
66 | private function initializeConfig() |
||
67 | { |
||
68 | try { |
||
69 | // retrieve last modified time for config file (filemtime returns int|bool!) |
||
70 | $configModifiedTime = filemtime($this->filePath); |
||
71 | if (!is_bool($configModifiedTime)) { |
||
|
|||
72 | $this->configModifiedTime = $configModifiedTime; |
||
73 | } |
||
74 | // use APC user cache to store parsed config.ttl configuration |
||
75 | if ($this->cache->isAvailable() && !is_null($this->configModifiedTime)) { |
||
76 | // @codeCoverageIgnoreStart |
||
77 | $key = realpath($this->filePath) . ", " . $this->configModifiedTime; |
||
78 | $nskey = "namespaces of " . $key; |
||
79 | $this->graph = $this->cache->fetch($key); |
||
80 | $this->namespaces = $this->cache->fetch($nskey); |
||
81 | if ($this->graph === false || $this->namespaces === false) { // was not found in cache |
||
82 | $this->parseConfig($this->filePath); |
||
83 | $this->cache->store($key, $this->graph); |
||
84 | $this->cache->store($nskey, $this->namespaces); |
||
85 | } |
||
86 | // @codeCoverageIgnoreEnd |
||
87 | } else { // APC not available, parse on every request |
||
88 | $this->parseConfig($this->filePath); |
||
89 | } |
||
90 | |||
91 | $configResources = $this->graph->allOfType("skosmos:Configuration"); |
||
92 | if (is_null($configResources) || !is_array($configResources) || count($configResources) !== 1) { |
||
93 | throw new Exception("config.ttl must have exactly one skosmos:Configuration"); |
||
94 | } |
||
95 | |||
96 | $this->resource = $configResources[0]; |
||
97 | $this->initializeNamespaces(); |
||
98 | } catch (Exception $e) { |
||
99 | echo "Error: " . $e->getMessage(); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Parses configuration from the config.ttl file |
||
105 | * @param string $filename path to config.ttl file |
||
106 | * @throws \EasyRdf\Exception |
||
107 | */ |
||
108 | private function parseConfig($filename) |
||
109 | { |
||
110 | $this->graph = new EasyRdf\Graph(); |
||
111 | $parser = new SkosmosTurtleParser(); |
||
112 | $parser->parse($this->graph, file_get_contents($filename), 'turtle', $filename); |
||
113 | $this->namespaces = $parser->getNamespaces(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Returns the graph created after parsing the configuration file. |
||
118 | * @return \EasyRdf\Graph |
||
119 | */ |
||
120 | public function getGraph() |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Registers RDF namespaces from the config.ttl file for use by EasyRdf (e.g. serializing) |
||
127 | */ |
||
128 | private function initializeNamespaces() { |
||
129 | foreach ($this->namespaces as $prefix => $fullUri) { |
||
130 | if ($prefix != '' && EasyRdf\RdfNamespace::get($prefix) === null) // if not already defined |
||
131 | { |
||
132 | EasyRdf\RdfNamespace::set($prefix, $fullUri); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns the UI languages specified in the configuration or defaults to |
||
139 | * only show English |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getLanguages() |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Returns the external HTTP request timeout in seconds or the default value |
||
164 | * of 5 seconds if not specified in the configuration. |
||
165 | * @return integer |
||
166 | */ |
||
167 | public function getHttpTimeout() |
||
168 | { |
||
169 | return $this->getLiteral('skosmos:httpTimeout', 5); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Returns the SPARQL HTTP request timeout in seconds or the default value |
||
174 | * of 20 seconds if not specified in the configuration. |
||
175 | * @return integer |
||
176 | */ |
||
177 | public function getSparqlTimeout() |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Returns the sparql endpoint address defined in the configuration. If |
||
184 | * not then defaulting to http://localhost:3030/ds/sparql |
||
185 | * @return string |
||
186 | */ |
||
187 | public function getDefaultEndpoint() |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Returns the maximum number of items to return in transitive queries if defined |
||
201 | * in the configuration or the default value of 1000. |
||
202 | * @return integer |
||
203 | */ |
||
204 | public function getDefaultTransitiveLimit() |
||
205 | { |
||
206 | return $this->getLiteral('skosmos:transitiveLimit', 1000); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Returns the maximum number of items to load at a time if defined |
||
211 | * in the configuration or the default value of 20. |
||
212 | * @return integer |
||
213 | */ |
||
214 | public function getSearchResultsSize() |
||
215 | { |
||
216 | return $this->getLiteral('skosmos:searchResultsSize', 20); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns the configured location for the twig template cache and if not |
||
221 | * defined defaults to "/tmp/skosmos-template-cache" |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getTemplateCache() |
||
225 | { |
||
226 | return $this->getLiteral('skosmos:templateCache', '/tmp/skosmos-template-cache'); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Returns the defined sparql-query extension eg. "JenaText" or |
||
231 | * if not defined falling back to SPARQL 1.1 |
||
232 | * @return string |
||
233 | */ |
||
234 | public function getDefaultSparqlDialect() |
||
235 | { |
||
236 | return $this->getLiteral('skosmos:sparqlDialect', 'Generic'); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Returns the feedback address defined in the configuration. |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getFeedbackAddress() |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Returns the feedback sender address defined in the configuration. |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getFeedbackSender() |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Returns the feedback envelope sender address defined in the configuration. |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getFeedbackEnvelopeSender() |
||
262 | { |
||
263 | return $this->getLiteral('skosmos:feedbackEnvelopeSender', null); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Returns true if exception logging has been configured. |
||
268 | * @return boolean |
||
269 | */ |
||
270 | public function getLogCaughtExceptions() |
||
271 | { |
||
272 | return $this->getBoolean('skosmos:logCaughtExceptions', FALSE); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Returns true if browser console logging has been enabled, |
||
277 | * @return boolean |
||
278 | */ |
||
279 | public function getLoggingBrowserConsole() |
||
280 | { |
||
281 | return $this->getBoolean('skosmos:logBrowserConsole', FALSE); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Returns the name of a log file if configured, or NULL otherwise. |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getLoggingFilename() |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getServiceName() |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Returns the long version of the service name in the requested language. |
||
303 | * @return string the long name of the service |
||
304 | */ |
||
305 | public function getServiceNameLong($lang) |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Returns the service description in the requested language. |
||
319 | * @return string the description of the service |
||
320 | */ |
||
321 | public function getServiceDescription($lang) |
||
322 | { |
||
323 | return $this->getLiteral('skosmos:serviceDescription', null, $lang); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Returns the feedback page description in the requested language. |
||
328 | * @return string the description of the feedback page |
||
329 | */ |
||
330 | public function getFeedbackDescription($lang) |
||
331 | { |
||
332 | return $this->getLiteral('skosmos:feedbackDescription', null, $lang); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Returns the about page description in the requested language. |
||
337 | * @return string the description of the about page |
||
338 | */ |
||
339 | public function getAboutDescription($lang) |
||
340 | { |
||
341 | return $this->getLiteral('skosmos:aboutDescription', null, $lang); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @return string |
||
346 | */ |
||
347 | public function getCustomCss() |
||
348 | { |
||
349 | return $this->getLiteral('skosmos:customCss', null); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return boolean |
||
354 | */ |
||
355 | public function getUiLanguageDropdown() |
||
356 | { |
||
357 | return $this->getBoolean('skosmos:uiLanguageDropdown', FALSE); |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @return string |
||
362 | */ |
||
363 | public function getBaseHref() |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @return array |
||
370 | */ |
||
371 | public function getGlobalPlugins() |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @return boolean |
||
385 | */ |
||
386 | public function getHoneypotEnabled() |
||
387 | { |
||
388 | return $this->getBoolean('skosmos:uiHoneypotEnabled', TRUE); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @return integer |
||
393 | */ |
||
394 | public function getHoneypotTime() |
||
395 | { |
||
396 | return $this->getLiteral('skosmos:uiHoneypotTime', 5); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @return boolean |
||
401 | */ |
||
402 | public function getCollationEnabled() |
||
405 | } |
||
406 | } |
||
407 |