[icinga-checkins] icinga.org: icingaweb2/feature/announce-banner-11198: IniRepository: Reduce complexity of method getDataSource
git at icinga.org
git at icinga.org
Fri Nov 4 10:50:04 CET 2016
Module: icingaweb2
Branch: feature/announce-banner-11198
Commit: ad79e675508b6e39ddac45414f7dc770eac008de
URL: https://git.icinga.org/?p=icingaweb2.git;a=commit;h=ad79e675508b6e39ddac45414f7dc770eac008de
Author: Johannes Meyer <johannes.meyer at netways.de>
Date: Fri Nov 4 10:17:37 2016 +0100
IniRepository: Reduce complexity of method getDataSource
refs #13034
---
library/Icinga/Repository/IniRepository.php | 96 ++++++++++++---------------
1 file changed, 42 insertions(+), 54 deletions(-)
diff --git a/library/Icinga/Repository/IniRepository.php b/library/Icinga/Repository/IniRepository.php
index 2005130..3c30a5e 100644
--- a/library/Icinga/Repository/IniRepository.php
+++ b/library/Icinga/Repository/IniRepository.php
@@ -74,59 +74,56 @@ abstract class IniRepository extends Repository implements Extensible, Updatable
/**
* {@inheritDoc}
+ *
+ * @return Config
*/
public function getDataSource($table = null)
{
- if ($this->ds === null) {
- if ($table === null) {
- $table = $this->getBaseTable();
- }
-
- if ($this->configs === null) {
- $this->configs = $this->initializeConfigs();
- if ($this->configs === null) {
- throw new ProgrammingError('Per-table configs missing');
- }
- }
+ if ($this->ds !== null) {
+ return parent::getDataSource($table);
+ }
- if (! isset($this->configs[$table])) {
- throw new ProgrammingError('Config for table "%s" missing', $table);
- }
+ $table = $table ?: $this->getBaseTable();
+ $configs = $this->getConfigs();
+ if (! isset($configs[$table])) {
+ throw new ProgrammingError('Config for table "%s" missing', $table);
+ } elseif (! $configs[$table] instanceof Config) {
+ $configs[$table] = $this->createConfig($configs[$table], $table);
+ }
- $config = $this->configs[$table];
- if ($config instanceof Config) {
- if (! $config->getConfigObject()->getKeyColumn()) {
- throw new ProgrammingError(
- 'INI repositories require their data source to provide a valid key column'
- );
- }
- } elseif (is_array($config)) {
- if (! isset($config['path'])) {
- throw new ProgrammingError('Path to config for table "%s" missing', $table);
- }
- if (! isset($config['keyColumn'])) {
- throw new ProgrammingError(
- 'INI repositories require their data source to provide a valid key column'
- );
- }
+ if (! $configs[$table]->getConfigObject()->getKeyColumn()) {
+ throw new ProgrammingError(
+ 'INI repositories require their data source to provide a valid key column'
+ );
+ }
- $newConfig = isset($config['module'])
- ? Config::module($config['module'], $config['path'])
- : Config::app($config['path']);
- $newConfig->getConfigObject()->setKeyColumn($config['keyColumn']);
- $this->configs[$table] = $config = $newConfig;
- } else {
- throw new ProgrammingError(
- 'Config for table "%s" is a %s, expected either Icinga\Application\Config or an associative array',
- $table,
- is_object($config) ? get_class($config) : gettype($config)
- );
- }
+ return $configs[$table];
+ }
- return $config;
- } else {
- return parent::getDataSource($table);
+ /**
+ * Return the configuration files used as table specific datasources
+ *
+ * Calls $this->initializeConfigs() in case $this->configs is null.
+ *
+ * @return array
+ */
+ public function getConfigs()
+ {
+ if ($this->configs === null) {
+ $this->configs = $this->initializeConfigs();
}
+
+ return $this->configs;
+ }
+
+ /**
+ * Overwrite this in your repository implementation in case you need to initialize the configs lazily
+ *
+ * @return array
+ */
+ protected function initializeConfigs()
+ {
+ return array();
}
/**
@@ -156,15 +153,6 @@ abstract class IniRepository extends Repository implements Extensible, Updatable
}
/**
- * Overwrite this in your INI repository implementation in case you need to initialize the configs lazily
- *
- * @return array
- */
- protected function initializeConfigs()
- {
- }
-
- /**
* Run a trigger for the given table and row which is about to be inserted
*
* @param string $table
More information about the icinga-checkins
mailing list