Sync: fetch properties at construction time

This commit is contained in:
Thomas Gelf 2016-02-23 13:03:47 +01:00
parent ff7e243370
commit 0b1f090972

View File

@ -25,6 +25,8 @@ class Sync
protected $destinationKeyPattern; protected $destinationKeyPattern;
protected $syncProperties;
/** /**
* Constructor. No direct initialization allowed right now. Please use one * Constructor. No direct initialization allowed right now. Please use one
* of the available static factory methods * of the available static factory methods
@ -33,6 +35,7 @@ class Sync
{ {
$this->rule = $rule; $this->rule = $rule;
$this->db = $rule->getConnection(); $this->db = $rule->getConnection();
$this->syncProperties = $rule->fetchSyncProperties();
} }
/** /**
@ -202,10 +205,10 @@ class Sync
return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string); return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
} }
protected function perpareImportSources($properties) protected function perpareImportSources()
{ {
$sources = array(); $sources = array();
foreach ($properties as $p) { foreach ($this->syncProperties as $p) {
$sourceId = $p->source_id; $sourceId = $p->source_id;
if (! array_key_exists($sourceId, $sources)) { if (! array_key_exists($sourceId, $sources)) {
$sources[$sourceId] = ImportSource::load($sourceId, $this->db); $sources[$sourceId] = ImportSource::load($sourceId, $this->db);
@ -215,12 +218,12 @@ class Sync
return $sources; return $sources;
} }
protected function prepareSourceColumns($properties) protected function prepareSourceColumns()
{ {
// $fieldMap = array(); // $fieldMap = array();
$columns = array(); $columns = array();
foreach ($properties as $p) { foreach ($this->syncProperties as $p) {
$sourceId = $p->source_id; $sourceId = $p->source_id;
if (! array_key_exists($sourceId, $columns)) { if (! array_key_exists($sourceId, $columns)) {
$columns[$sourceId] = array(); $columns[$sourceId] = array();
@ -244,7 +247,7 @@ class Sync
if ($this->rule->object_type === 'service') { if ($this->rule->object_type === 'service') {
$hasHost = false; $hasHost = false;
$hasObjectName = false; $hasObjectName = false;
foreach ($properties as $key => $property) { foreach ($this->syncProperties as $key => $property) {
if ($property->destination_field === 'host') { if ($property->destination_field === 'host') {
$hasHost = $property->source_expression; $hasHost = $property->source_expression;
} }
@ -264,12 +267,12 @@ class Sync
return $this->hasCombinedKey; return $this->hasCombinedKey;
} }
protected function fetchImportedData($sources, $properties) protected function fetchImportedData($sources)
{ {
$imported = array(); $imported = array();
$rule = $this->rule; $rule = $this->rule;
$sourceColumns = $this->prepareSourceColumns($properties); $sourceColumns = $this->prepareSourceColumns();
foreach ($sources as $source) { foreach ($sources as $source) {
$sourceId = $source->id; $sourceId = $source->id;
@ -320,10 +323,10 @@ class Sync
} }
// TODO: This is rubbish, we need to filter at fetch time // TODO: This is rubbish, we need to filter at fetch time
protected function removeForeignListEntries(& $objects, & $properties) protected function removeForeignListEntries(& $objects)
{ {
$listId = null; $listId = null;
foreach ($properties as $prop) { foreach ($this->syncProperties as $prop) {
if ($prop->destination_field === 'list_id') { if ($prop->destination_field === 'list_id') {
$listId = (int) $prop->source_expression; $listId = (int) $prop->source_expression;
} }
@ -347,7 +350,7 @@ class Sync
} }
} }
protected function prepareNewObjects(& $properties, & $sources, & $imported) protected function prepareNewObjects(& $sources, & $imported)
{ {
$newObjects = array(); $newObjects = array();
@ -360,7 +363,7 @@ class Sync
$newVars = array(); $newVars = array();
$imports = array(); $imports = array();
foreach ($properties as $p) { foreach ($this->syncProperties as $p) {
if ($p->source_id !== $sourceId) continue; if ($p->source_id !== $sourceId) continue;
$prop = $p->destination_field; $prop = $p->destination_field;
@ -435,21 +438,19 @@ class Sync
protected function prepare() protected function prepare()
{ {
$rule = $this->rule; $rule = $this->rule;
$properties = $rule->fetchSyncProperties(); $sources = $this->perpareImportSources();
$sources = $this->perpareImportSources($properties); $imported = $this->fetchImportedData($sources);
$imported = $this->fetchImportedData($sources, $properties);
// TODO: Make object_type (template, object...) and object_name mandatory? // TODO: Make object_type (template, object...) and object_name mandatory?
$objects = IcingaObject::loadAllByType($rule->object_type, $this->db); $objects = IcingaObject::loadAllByType($rule->object_type, $this->db);
// TODO: should be obsoleted by a better "loadFiltered" method // TODO: should be obsoleted by a better "loadFiltered" method
if ($rule->object_type === 'datalistEntry') { if ($rule->object_type === 'datalistEntry') {
$this->removeForeignListEntries($objects, $properties); $this->removeForeignListEntries($objects);
} }
// TODO: directly work on existing objects, remember imported keys, then purge // TODO: directly work on existing objects, remember imported keys, then purge
$newObjects = $this->prepareNewObjects( $newObjects = $this->prepareNewObjects(
$properties,
$sources, $sources,
$imported $imported
); );