| Total Complexity | 64 |
| Total Lines | 433 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 27 | class GlobalConfig extends BaseConfig |
||
| 28 | { |
||
| 29 | /** Cache reference */ |
||
| 30 | private $cache; |
||
| 31 | /** Location of the configuration file. Used for caching. */ |
||
| 32 | private $filePath; |
||
| 33 | /** Namespaces from vocabularies configuration file. */ |
||
| 34 | private $namespaces; |
||
| 35 | /** EasyRdf\Graph graph */ |
||
| 36 | private $graph; |
||
| 37 | /** |
||
| 38 | * @var int the time the config file was last modified |
||
| 39 | */ |
||
| 40 | private $configModifiedTime = null; |
||
| 41 | |||
| 42 | private static function getCheckedConfigFileRealPath(string $path): string |
||
| 43 | { |
||
| 44 | if (str_starts_with($path, '/')) { |
||
| 45 | $realpath = realpath($path); |
||
| 46 | } else { |
||
| 47 | $realpath = realpath(dirname(__FILE__) . "/" . $path); |
||
| 48 | } |
||
| 49 | if (!$realpath) { |
||
| 50 | throw new ConfigFileNotFoundException($path); |
||
| 51 | } |
||
| 52 | return $realpath; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Gets the configuration file path. |
||
| 57 | * |
||
| 58 | * Returns the full path to the configuration file. If a config name is provided, |
||
| 59 | * it will be used to construct the path. Otherwise, fallback path will be used. |
||
| 60 | * |
||
| 61 | * First fallback is the `SKOSMOS_CONFIG_NAME` environment variable, |
||
| 62 | * which accepts two formats: |
||
| 63 | * - Absolute path: A full path to the configuration file (e.g., /etc/skosmos/config.ttl) |
||
| 64 | * - Relative path: A path relative to the application root (e.g., config/config.ttl) |
||
| 65 | * |
||
| 66 | * Second fallback is "config.ttl" path in the root directory. |
||
| 67 | * |
||
| 68 | * @param string|null $config_name Optional configuration file name |
||
| 69 | * @return string The full path to the configuration file. Throws errors on failure, |
||
| 70 | * e.g. if the file does not exist. |
||
| 71 | */ |
||
| 72 | public static function getConfigFilePath(?string $config_name = null) |
||
| 73 | { |
||
| 74 | $path = '../../config.ttl'; |
||
| 75 | if (isset($config_name)) { |
||
| 76 | $path = $config_name; |
||
| 77 | } elseif (getenv('SKOSMOS_CONFIG_NAME')) { |
||
| 78 | if (str_starts_with(getenv('SKOSMOS_CONFIG_NAME'), '/')) { |
||
| 79 | $path = getenv('SKOSMOS_CONFIG_NAME'); |
||
| 80 | } else { |
||
| 81 | $path = dirname(__FILE__) . '/../../' . getenv('SKOSMOS_CONFIG_NAME'); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | return GlobalConfig::getCheckedConfigFileRealPath($path); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function __construct(Model $model, ?string $config_name = null) |
||
| 88 | { |
||
| 89 | $this->cache = new Cache(); |
||
| 90 | $this->filePath = GlobalConfig::getConfigFilePath($config_name); |
||
| 91 | $resource = $this->initializeConfig(); |
||
| 92 | parent::__construct($model, $resource); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getCache() |
||
| 96 | { |
||
| 97 | return $this->cache; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return int the time the config file was last modified |
||
| 102 | */ |
||
| 103 | public function getConfigModifiedTime() |
||
| 104 | { |
||
| 105 | return $this->configModifiedTime; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Initialize configuration, reading the configuration file from the disk, |
||
| 110 | * and creating the graph and resources objects. Uses a cache if available, |
||
| 111 | * in order to avoid re-loading the complete configuration on each request. |
||
| 112 | */ |
||
| 113 | private function initializeConfig(): EasyRdf\Resource |
||
| 114 | { |
||
| 115 | // retrieve last modified time for config file (filemtime returns int|bool!) |
||
| 116 | $configModifiedTime = filemtime($this->filePath); |
||
| 117 | if (!is_bool($configModifiedTime)) { |
||
|
|
|||
| 118 | $this->configModifiedTime = $configModifiedTime; |
||
| 119 | } |
||
| 120 | // use APC user cache to store parsed config.ttl configuration |
||
| 121 | if ($this->cache->isAvailable() && !is_null($this->configModifiedTime)) { |
||
| 122 | // @codeCoverageIgnoreStart |
||
| 123 | $key = realpath($this->filePath) . ", " . $this->configModifiedTime; |
||
| 124 | $nskey = "namespaces of " . $key; |
||
| 125 | $this->graph = $this->cache->fetch($key); |
||
| 126 | $this->namespaces = $this->cache->fetch($nskey); |
||
| 127 | if ($this->graph === false || $this->namespaces === false) { // was not found in cache |
||
| 128 | $this->parseConfig($this->filePath); |
||
| 129 | $this->cache->store($key, $this->graph); |
||
| 130 | $this->cache->store($nskey, $this->namespaces); |
||
| 131 | } |
||
| 132 | // @codeCoverageIgnoreEnd |
||
| 133 | } else { // APC not available, parse on every request |
||
| 134 | $this->parseConfig($this->filePath); |
||
| 135 | } |
||
| 136 | $this->initializeNamespaces(); |
||
| 137 | |||
| 138 | $configResources = $this->graph->allOfType("skosmos:Configuration"); |
||
| 139 | if (is_null($configResources) || !is_array($configResources) || count($configResources) !== 1) { |
||
| 140 | throw new Exception("config.ttl must have exactly one skosmos:Configuration"); |
||
| 141 | } |
||
| 142 | return $configResources[0]; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Parses configuration from the config.ttl file |
||
| 147 | * @param string $filename path to config.ttl file |
||
| 148 | * @throws \EasyRdf\Exception |
||
| 149 | */ |
||
| 150 | private function parseConfig($filename) |
||
| 151 | { |
||
| 152 | $this->graph = new EasyRdf\Graph(); |
||
| 153 | $parser = new SkosmosTurtleParser(); |
||
| 154 | $parser->parse($this->graph, file_get_contents($filename), 'turtle', $filename); |
||
| 155 | $this->namespaces = $parser->getNamespaces(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns the graph created after parsing the configuration file. |
||
| 160 | * @return \EasyRdf\Graph |
||
| 161 | */ |
||
| 162 | public function getGraph() |
||
| 163 | { |
||
| 164 | return $this->graph; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Registers RDF namespaces from the config.ttl file for use by EasyRdf (e.g. serializing) |
||
| 169 | */ |
||
| 170 | private function initializeNamespaces() |
||
| 171 | { |
||
| 172 | foreach ($this->namespaces as $prefix => $fullUri) { |
||
| 173 | if ($prefix != '' && EasyRdf\RdfNamespace::get($prefix) === null) { // if not already defined |
||
| 174 | EasyRdf\RdfNamespace::set($prefix, $fullUri); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the UI languages specified in the configuration or defaults to |
||
| 181 | * only show English |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getLanguages() |
||
| 185 | { |
||
| 186 | $languageResources = $this->getResource()->getResource('skosmos:languages'); |
||
| 187 | if (!is_null($languageResources) && !empty($languageResources)) { |
||
| 188 | $languages = array(); |
||
| 189 | foreach ($languageResources as $languageResource) { |
||
| 190 | /** @var \EasyRdf\Literal $languageName */ |
||
| 191 | $languageName = $languageResource->getLiteral('rdfs:label'); |
||
| 192 | /** @var \EasyRdf\Literal $languageValue */ |
||
| 193 | $languageValue = $languageResource->getLiteral('rdf:value'); |
||
| 194 | if ($languageName && $languageValue) { |
||
| 195 | $languages[$languageName->getValue()] = $languageValue->getValue(); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | return $languages; |
||
| 199 | } else { |
||
| 200 | return array('en' => 'en_GB.utf8'); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the external HTTP request timeout in seconds or the default value |
||
| 206 | * of 5 seconds if not specified in the configuration. |
||
| 207 | * @return integer |
||
| 208 | */ |
||
| 209 | public function getHttpTimeout() |
||
| 210 | { |
||
| 211 | return $this->getLiteral('skosmos:httpTimeout', 5); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the SPARQL HTTP request timeout in seconds or the default value |
||
| 216 | * of 20 seconds if not specified in the configuration. |
||
| 217 | * @return integer |
||
| 218 | */ |
||
| 219 | public function getSparqlTimeout() |
||
| 220 | { |
||
| 221 | return $this->getLiteral('skosmos:sparqlTimeout', 20); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the sparql endpoint address defined in the configuration. If |
||
| 226 | * not then defaulting to http://localhost:3030/ds/sparql |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getDefaultEndpoint() |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the maximum number of items to return in transitive queries if defined |
||
| 243 | * in the configuration or the default value of 1000. |
||
| 244 | * @return integer |
||
| 245 | */ |
||
| 246 | public function getDefaultTransitiveLimit() |
||
| 247 | { |
||
| 248 | return $this->getLiteral('skosmos:transitiveLimit', 1000); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the maximum number of items to load at a time if defined |
||
| 253 | * in the configuration or the default value of 20. |
||
| 254 | * @return integer |
||
| 255 | */ |
||
| 256 | public function getSearchResultsSize() |
||
| 257 | { |
||
| 258 | return $this->getLiteral('skosmos:searchResultsSize', 20); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the configured location for the twig template cache and if not |
||
| 263 | * defined defaults to "/tmp/skosmos-template-cache" |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function getTemplateCache() |
||
| 267 | { |
||
| 268 | return $this->getLiteral('skosmos:templateCache', '/tmp/skosmos-template-cache'); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns the defined sparql-query extension eg. "JenaText" or |
||
| 273 | * if not defined falling back to SPARQL 1.1 |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getDefaultSparqlDialect() |
||
| 277 | { |
||
| 278 | return $this->getLiteral('skosmos:sparqlDialect', 'Generic'); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the feedback address defined in the configuration. |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | public function getFeedbackAddress() |
||
| 286 | { |
||
| 287 | return $this->getLiteral('skosmos:feedbackAddress', null); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns the feedback sender address defined in the configuration. |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getFeedbackSender() |
||
| 295 | { |
||
| 296 | return $this->getLiteral('skosmos:feedbackSender', null); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the feedback envelope sender address defined in the configuration. |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function getFeedbackEnvelopeSender() |
||
| 304 | { |
||
| 305 | return $this->getLiteral('skosmos:feedbackEnvelopeSender', null); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns true if exception logging has been configured. |
||
| 310 | * @return boolean |
||
| 311 | */ |
||
| 312 | public function getLogCaughtExceptions() |
||
| 313 | { |
||
| 314 | return $this->getBoolean('skosmos:logCaughtExceptions', false); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns true if browser console logging has been enabled, |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | public function getLoggingBrowserConsole() |
||
| 322 | { |
||
| 323 | return $this->getBoolean('skosmos:logBrowserConsole', false); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns the name of a log file if configured, or NULL otherwise. |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getLoggingFilename() |
||
| 331 | { |
||
| 332 | return $this->getLiteral('skosmos:logFileName', null); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getServiceName() |
||
| 339 | { |
||
| 340 | return $this->getLiteral('skosmos:serviceName', 'Skosmos'); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns the long version of the service name in the requested language. |
||
| 345 | * @return string the long name of the service |
||
| 346 | */ |
||
| 347 | public function getServiceNameLong($lang) |
||
| 348 | { |
||
| 349 | $val = $this->getLiteral('skosmos:serviceNameLong', false, $lang); |
||
| 350 | |||
| 351 | if ($val === false) { |
||
| 352 | // fall back to short service name if not configured |
||
| 353 | return $this->getServiceName(); |
||
| 354 | } |
||
| 355 | |||
| 356 | return $val; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the service description in the requested language. |
||
| 361 | * @return string the description of the service |
||
| 362 | */ |
||
| 363 | public function getServiceDescription($lang) |
||
| 364 | { |
||
| 365 | return $this->getLiteral('skosmos:serviceDescription', null, $lang); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Returns the feedback page description in the requested language. |
||
| 370 | * @return string the description of the feedback page |
||
| 371 | */ |
||
| 372 | public function getFeedbackDescription($lang) |
||
| 373 | { |
||
| 374 | return $this->getLiteral('skosmos:feedbackDescription', null, $lang); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Returns the about page description in the requested language. |
||
| 379 | * @return string the description of the about page |
||
| 380 | */ |
||
| 381 | public function getAboutDescription($lang) |
||
| 382 | { |
||
| 383 | return $this->getLiteral('skosmos:aboutDescription', null, $lang); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function getCustomCss() |
||
| 390 | { |
||
| 391 | return $this->getLiteral('skosmos:customCss', null); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return boolean |
||
| 396 | */ |
||
| 397 | public function getUiLanguageDropdown() |
||
| 398 | { |
||
| 399 | return $this->getBoolean('skosmos:uiLanguageDropdown', false); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | public function getUiDevMode() |
||
| 406 | { |
||
| 407 | return $this->getBoolean('skosmos:uiDevMode', false); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getBaseHref() |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | public function getGlobalPlugins() |
||
| 427 | { |
||
| 428 | $globalPlugins = array(); |
||
| 429 | $globalPluginsResource = $this->getResource()->getResource("skosmos:globalPlugins"); |
||
| 430 | if ($globalPluginsResource) { |
||
| 431 | foreach ($globalPluginsResource as $resource) { |
||
| 432 | $globalPlugins[] = $resource->getValue(); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | return $globalPlugins; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return boolean |
||
| 440 | */ |
||
| 441 | public function getHoneypotEnabled() |
||
| 442 | { |
||
| 443 | return $this->getBoolean('skosmos:feedbackHoneypotEnabled', true); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return integer |
||
| 448 | */ |
||
| 449 | public function getHoneypotTime() |
||
| 450 | { |
||
| 451 | return $this->getLiteral('skosmos:feedbackHoneypotTime', 5); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @return boolean |
||
| 456 | */ |
||
| 457 | public function getCollationEnabled() |
||
| 460 | } |
||
| 461 | } |
||
| 462 |