| Total Complexity | 51 |
| Total Lines | 342 |
| 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() |
||
| 57 | { |
||
| 58 | return $this->configModifiedTime; |
||
| 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() |
||
| 121 | { |
||
| 122 | return $this->graph; |
||
| 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() |
||
| 143 | { |
||
| 144 | $languageResources = $this->getResource()->getResource('skosmos:languages'); |
||
| 145 | if (!is_null($languageResources) && !empty($languageResources)) { |
||
| 146 | $languages = array(); |
||
| 147 | foreach ($languageResources as $languageResource) { |
||
| 148 | /** @var \EasyRdf\Literal $languageName */ |
||
| 149 | $languageName = $languageResource->getLiteral('rdfs:label'); |
||
| 150 | /** @var \EasyRdf\Literal $languageValue */ |
||
| 151 | $languageValue = $languageResource->getLiteral('rdf:value'); |
||
| 152 | if ($languageName && $languageValue) { |
||
| 153 | $languages[$languageName->getValue()] = $languageValue->getValue(); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | return $languages; |
||
| 157 | } else { |
||
| 158 | return array('en' => 'en_GB.utf8'); |
||
| 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() |
||
| 178 | { |
||
| 179 | return $this->getLiteral('skosmos:sparqlTimeout', 20); |
||
| 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() |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the maximum number of items to return in transitive queries if defined |
||
| 199 | * in the configuration or the default value of 1000. |
||
| 200 | * @return integer |
||
| 201 | */ |
||
| 202 | public function getDefaultTransitiveLimit() |
||
| 203 | { |
||
| 204 | return $this->getLiteral('skosmos:transitiveLimit', 1000); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the maximum number of items to load at a time if defined |
||
| 209 | * in the configuration or the default value of 20. |
||
| 210 | * @return integer |
||
| 211 | */ |
||
| 212 | public function getSearchResultsSize() |
||
| 213 | { |
||
| 214 | return $this->getLiteral('skosmos:searchResultsSize', 20); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the configured location for the twig template cache and if not |
||
| 219 | * defined defaults to "/tmp/skosmos-template-cache" |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function getTemplateCache() |
||
| 223 | { |
||
| 224 | return $this->getLiteral('skosmos:templateCache', '/tmp/skosmos-template-cache'); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the defined sparql-query extension eg. "JenaText" or |
||
| 229 | * if not defined falling back to SPARQL 1.1 |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function getDefaultSparqlDialect() |
||
| 233 | { |
||
| 234 | return $this->getLiteral('skosmos:sparqlDialect', 'Generic'); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns the feedback address defined in the configuration. |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getFeedbackAddress() |
||
| 242 | { |
||
| 243 | return $this->getLiteral('skosmos:feedbackAddress', null); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns the feedback sender address defined in the configuration. |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getFeedbackSender() |
||
| 251 | { |
||
| 252 | return $this->getLiteral('skosmos:feedbackSender', null); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns the feedback envelope sender address defined in the configuration. |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function getFeedbackEnvelopeSender() |
||
| 260 | { |
||
| 261 | return $this->getLiteral('skosmos:feedbackEnvelopeSender', null); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns true if exception logging has been configured. |
||
| 266 | * @return boolean |
||
| 267 | */ |
||
| 268 | public function getLogCaughtExceptions() |
||
| 269 | { |
||
| 270 | return $this->getBoolean('skosmos:logCaughtExceptions', FALSE); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns true if browser console logging has been enabled, |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | public function getLoggingBrowserConsole() |
||
| 278 | { |
||
| 279 | return $this->getBoolean('skosmos:logBrowserConsole', FALSE); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns the name of a log file if configured, or NULL otherwise. |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getLoggingFilename() |
||
| 287 | { |
||
| 288 | return $this->getLiteral('skosmos:logFileName', null); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getServiceName() |
||
| 295 | { |
||
| 296 | return $this->getLiteral('skosmos:serviceName', 'Skosmos'); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function getCustomCss() |
||
| 303 | { |
||
| 304 | return $this->getLiteral('skosmos:customCss', null); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return boolean |
||
| 309 | */ |
||
| 310 | public function getUiLanguageDropdown() |
||
| 311 | { |
||
| 312 | return $this->getBoolean('skosmos:uiLanguageDropdown', FALSE); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getBaseHref() |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | public function getGlobalPlugins() |
||
| 327 | { |
||
| 328 | $globalPlugins = array(); |
||
| 329 | $globalPluginsResource = $this->getResource()->getResource("skosmos:globalPlugins"); |
||
| 330 | if ($globalPluginsResource) { |
||
| 331 | foreach ($globalPluginsResource as $resource) { |
||
| 332 | $globalPlugins[] = $resource->getValue(); |
||
| 333 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | public function getHoneypotEnabled() |
||
| 342 | { |
||
| 343 | return $this->getBoolean('skosmos:uiHoneypotEnabled', TRUE); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return integer |
||
| 348 | */ |
||
| 349 | public function getHoneypotTime() |
||
| 350 | { |
||
| 351 | return $this->getLiteral('skosmos:uiHoneypotTime', 5); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return boolean |
||
| 356 | */ |
||
| 357 | public function getCollationEnabled() |
||
| 360 | } |
||
| 361 | } |
||
| 362 |