[icinga-checkins] icinga.org: icingaweb2-module-director/fiddle/mfrosch: ImportSource: take over modifier loading logic

git at icinga.org git at icinga.org
Fri Jul 22 13:55:47 CEST 2016


Module: icingaweb2-module-director
Branch: fiddle/mfrosch
Commit: 38bf19eb92420421c5daccefa8a201281ec9ad94
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=38bf19eb92420421c5daccefa8a201281ec9ad94

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Jul 20 13:40:43 2016 +0200

ImportSource: take over modifier loading logic

---

 application/tables/ImportsourceHookTable.php   |   18 ++--------------
 library/Director/Hook/PropertyModifierHook.php |   23 ++-------------------
 library/Director/Import/Import.php             |   19 +----------------
 library/Director/Objects/ImportSource.php      |   26 ++++++++++++++++++++++++
 4 files changed, 31 insertions(+), 55 deletions(-)

diff --git a/application/tables/ImportsourceHookTable.php b/application/tables/ImportsourceHookTable.php
index 1cc4529..02c380b 100644
--- a/application/tables/ImportsourceHookTable.php
+++ b/application/tables/ImportsourceHookTable.php
@@ -76,24 +76,10 @@ class ImportsourceHookTable extends QuickTable
 
     protected function applyModifiers()
     {
-        $modifiers = $this->source->fetchRowModifiers();
-        if (empty($modifiers)) {
-            return;
-        }
-
-        $propertyModifiers = array();
-
-
-        foreach ($modifiers as $mod) {
-            if (! array_key_exists($mod->property_name, $propertyModifiers)) {
-                $propertyModifiers[$mod->property_name] = array();
-            }
-
-            $propertyModifiers[$mod->property_name][] = $mod->getInstance();
-        }
+        $modifiers = $this->source->getRowModifiers();
 
         foreach ($this->dataCache as & $row) {
-            foreach ($propertyModifiers as $key => $mods) {
+            foreach ($modifiers as $key => $mods) {
                 foreach ($mods as $mod) {
                     if (is_array($row->$key)) {
                         foreach ($row->$key as & $k) {
diff --git a/library/Director/Hook/PropertyModifierHook.php b/library/Director/Hook/PropertyModifierHook.php
index 4fc4196..c5d2751 100644
--- a/library/Director/Hook/PropertyModifierHook.php
+++ b/library/Director/Hook/PropertyModifierHook.php
@@ -26,28 +26,9 @@ abstract class PropertyModifierHook
         return $class;
     }
 
-    public static function loadById($property_id, Db $connection)
+    public function getTargetProperty()
     {
-        $db = $connection->getDbAdapter();
-        $modifier = $db->fetchRow(
-            $db->select()->from(
-                'import_row_modifier',
-                array('id', 'provider_class')
-            )->where('property_id = ?', $property_id)
-        );
-
-        $settings = $db->fetchPairs(
-            $db->select()->from(
-                'import_row_modifier_settings',
-                array('setting_name', 'setting_value')
-            )->where('modifier_id = ?', $modifier->id)
-        );
-
-        $obj = new $modifier->provider_class;
-        $obj->setSettings($settings);
-        $obj->setDb($db);
-
-        return $obj;
+        return null;
     }
 
     public function setDb(Db $db)
diff --git a/library/Director/Import/Import.php b/library/Director/Import/Import.php
index 4d607a2..fd162b8 100644
--- a/library/Director/Import/Import.php
+++ b/library/Director/Import/Import.php
@@ -166,7 +166,7 @@ class Import
             )->fetchData();
         }
 
-        $modifiers = $this->prepareModifiers();
+        $modifiers = $this->source->getRowModifiers();
 
         if (empty($modifiers)) {
             return $this->data;
@@ -224,23 +224,6 @@ class Import
         return $this->properties[$checksum];
     }
 
-    // TODO: move to ImportSource, this duplicates logic from preview
-    protected function prepareModifiers()
-    {
-        $modifiers = $this->source->fetchRowModifiers();
-        $propertyModifiers = array();
-
-        foreach ($modifiers as $mod) {
-            if (! array_key_exists($mod->property_name, $propertyModifiers)) {
-                $propertyModifiers[$mod->property_name] = array();
-            }
-
-            $propertyModifiers[$mod->property_name][] = $mod->getInstance();
-        }
-
-        return $propertyModifiers;
-    }
-
     /**
      * Walk through each row, prepare properties and calculate checksums
      */
diff --git a/library/Director/Objects/ImportSource.php b/library/Director/Objects/ImportSource.php
index 12a5589..f1ffe78 100644
--- a/library/Director/Objects/ImportSource.php
+++ b/library/Director/Objects/ImportSource.php
@@ -30,6 +30,8 @@ class ImportSource extends DbObjectWithSettings
 
     protected $settingsRemoteId = 'source_id';
 
+    private $rowModifiers;
+
     public function fetchLastRun($required = false)
     {
         return $this->fetchLastRunBefore(time() + 1, $required);
@@ -75,6 +77,15 @@ class ImportSource extends DbObjectWithSettings
         return null;
     }
 
+    public function getRowModifiers()
+    {
+        if ($this->rowModifiers === null) {
+            $this->prepareRowModifiers();
+        }
+
+        return $this->rowModifiers;
+    }
+
     public function fetchRowModifiers()
     {
         $db = $this->getDb();
@@ -87,6 +98,21 @@ class ImportSource extends DbObjectWithSettings
         );
     }
 
+    protected function prepareRowModifiers()
+    {
+        $modifiers = array();
+
+        foreach ($this->fetchRowModifiers() as $mod) {
+            if (! array_key_exists($mod->property_name, $modifiers)) {
+                $modifiers[$mod->property_name] = array();
+            }
+
+            $modifiers[$mod->property_name][] = $mod->getInstance();
+        }
+
+        $this->rowModifiers = $modifiers;
+    }
+
     public function checkForChanges($runImport = false)
     {
         $hadChanges = false;



More information about the icinga-checkins mailing list