[icinga-checkins] icinga.org: icingaweb2-module-director/fiddle/mfrosch: ImportSource: take over duplicated transform 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: 29564ab742f13c5b9cfa0236bfb7d10d89570e62
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=29564ab742f13c5b9cfa0236bfb7d10d89570e62

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Jul 20 14:19:20 2016 +0200

ImportSource: take over duplicated transform logic

---

 application/tables/ImportsourceHookTable.php |   24 +++++------------------
 library/Director/Import/Import.php           |   23 +++-------------------
 library/Director/Objects/ImportSource.php    |   27 ++++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 39 deletions(-)

diff --git a/application/tables/ImportsourceHookTable.php b/application/tables/ImportsourceHookTable.php
index 02c380b..1f142c0 100644
--- a/application/tables/ImportsourceHookTable.php
+++ b/application/tables/ImportsourceHookTable.php
@@ -68,29 +68,15 @@ class ImportsourceHookTable extends QuickTable
             }
 
             $this->dataCache = $query->fetchAll();
-            $this->applyModifiers();
-        }
-
-        return $this->dataCache;
-    }
 
-    protected function applyModifiers()
-    {
-        $modifiers = $this->source->getRowModifiers();
-
-        foreach ($this->dataCache as & $row) {
-            foreach ($modifiers as $key => $mods) {
-                foreach ($mods as $mod) {
-                    if (is_array($row->$key)) {
-                        foreach ($row->$key as & $k) {
-                            $k = $mod->transform($k);
-                        }
-                    } else {
-                        $row->$key = $mod->transform($row->$key);
-                    }
+            if ($this->source->hasRowModifiers()) {
+                foreach ($this->dataCache as & $row) {
+                    $this->source->applyModifiersToRow($row);
                 }
             }
         }
+
+        return $this->dataCache;
     }
 
     public function getBaseQuery()
diff --git a/library/Director/Import/Import.php b/library/Director/Import/Import.php
index fd162b8..c90d701 100644
--- a/library/Director/Import/Import.php
+++ b/library/Director/Import/Import.php
@@ -166,26 +166,9 @@ class Import
             )->fetchData();
         }
 
-        $modifiers = $this->source->getRowModifiers();
-
-        if (empty($modifiers)) {
-            return $this->data;
-        }
-
-        foreach ($this->data as & $row) {
-            foreach ($modifiers as $key => $mods) {
-                foreach ($mods as $mod) {
-                    if (! property_exists($row, $key)) {
-                        continue;
-                    }
-                    if (is_array($row->$key)) {
-                        foreach ($row->$key as & $k) {
-                            $k = $mod->transform($k);
-                        }
-                    } else {
-                        $row->$key = $mod->transform($row->$key);
-                    }
-                }
+        if ($this->source->hasRowModifiers()) {
+            foreach ($this->data as & $row) {
+                $this->source->applyModifiersToRow($row);
             }
         }
 
diff --git a/library/Director/Objects/ImportSource.php b/library/Director/Objects/ImportSource.php
index f1ffe78..fc2dd30 100644
--- a/library/Director/Objects/ImportSource.php
+++ b/library/Director/Objects/ImportSource.php
@@ -77,6 +77,28 @@ class ImportSource extends DbObjectWithSettings
         return null;
     }
 
+    public function applyModifiersToRow(& $row)
+    {
+        $modifiers = $this->getRowModifiers();
+
+        foreach ($modifiers as $key => $mods) {
+            foreach ($mods as $mod) {
+                if (! property_exists($row, $key)) {
+                    continue;
+                }
+                if (is_array($row->$key)) {
+                    foreach ($row->$key as & $k) {
+                        $k = $mod->transform($k);
+                    }
+                } else {
+                    $row->$key = $mod->transform($row->$key);
+                }
+            }
+        }
+
+        return $this;
+    }
+
     public function getRowModifiers()
     {
         if ($this->rowModifiers === null) {
@@ -86,6 +108,11 @@ class ImportSource extends DbObjectWithSettings
         return $this->rowModifiers;
     }
 
+    public function hasRowModifiers()
+    {
+        return count($this->getRowModifiers()) > 0;
+    }
+
     public function fetchRowModifiers()
     {
         $db = $this->getDb();



More information about the icinga-checkins mailing list