From fc45de5f317a7593204be732a0e8b054ffe008e1 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 23 Feb 2016 17:47:18 +0100 Subject: [PATCH] Sync: same for objects --- library/Director/Import/Sync.php | 56 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php index bb91aa18..c3fbc1f0 100644 --- a/library/Director/Import/Sync.php +++ b/library/Director/Import/Sync.php @@ -26,6 +26,13 @@ class Sync */ protected $imported; + /** + * Objects to work with + * + * @var array + */ + protected $objects; + protected $modify = array(); protected $remove = array(); @@ -230,7 +237,7 @@ class Sync $this->sources = array(); foreach ($this->syncProperties as $p) { $id = $p->source_id; - if (! array_key_exists($id, $sources)) { + if (! array_key_exists($id, $this->sources)) { $this->sources[$id] = ImportSource::load($id, $this->db); } } @@ -348,7 +355,7 @@ class Sync } // TODO: This is rubbish, we need to filter at fetch time - protected function removeForeignListEntries(& $objects) + protected function removeForeignListEntries() { $listId = null; foreach ($this->syncProperties as $prop) { @@ -364,17 +371,33 @@ class Sync } $no = array(); - foreach ($objects as $k => $o) { + foreach ($this->objects as $k => $o) { if ($o->list_id !== $listId) { $no[] = $k; } } foreach ($no as $k) { - unset($objects[$k]); + unset($this->objects[$k]); } } + protected function loadExistingObjects() + { + // TODO: Make object_type (template, object...) and object_name mandatory? + $this->objects = IcingaObject::loadAllByType( + $this->rule->object_type, + $this->db + ); + + // TODO: should be obsoleted by a better "loadFiltered" method + if ($this->rule->object_type === 'datalistEntry') { + $this->removeForeignListEntries($this->objects); + } + + return $this; + } + protected function prepareNewObjects() { $newObjects = array(); @@ -464,44 +487,37 @@ class Sync { $rule = $this->rule; $this->prepareRelatedImportSources() - ->fetchImportedData(); - - // TODO: Make object_type (template, object...) and object_name mandatory? - $objects = IcingaObject::loadAllByType($rule->object_type, $this->db); - - // TODO: should be obsoleted by a better "loadFiltered" method - if ($rule->object_type === 'datalistEntry') { - $this->removeForeignListEntries($objects); - } + ->fetchImportedData() + ->loadExistingObjects(); // TODO: directly work on existing objects, remember imported keys, then purge $newObjects = $this->prepareNewObjects(); foreach ($newObjects as $key => $object) { - if (array_key_exists($key, $objects)) { + if (array_key_exists($key, $this->objects)) { switch ($rule->update_policy) { case 'override': - $objects[$key]->replaceWith($object); + $this->objects[$key]->replaceWith($object); break; case 'merge': // TODO: re-evaluate merge settings. vars.x instead of // just "vars" might suffice. - $objects[$key]->setProperties($object); + $this->objects[$key]->setProperties($object); break; default: // policy 'ignore', no action } } else { - $objects[$key] = $object; + $this->objects[$key] = $object; } } $objectKey = $rule->object_type === 'datalistEntry' ? 'entry_name' : 'object_name'; $noAction = array(); - foreach ($objects as $key => $object) { + foreach ($this->objects as $key => $object) { if (array_key_exists($key, $newObjects)) { // Stats? @@ -517,10 +533,10 @@ class Sync } foreach ($noAction as $key) { - unset($objects[$key]); + unset($this->objects[$key]); } - return $objects; + return $this->objects; } /**