diff options
Diffstat (limited to '')
-rw-r--r-- | library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php b/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php index 4653255..e565f77 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php @@ -4,7 +4,10 @@ namespace Icinga\Module\Director\DirectorObject\Automation; use Icinga\Module\Director\Db; use Icinga\Module\Director\Objects\DirectorDatafield; +use Icinga\Module\Director\Objects\DirectorDatalist; use Icinga\Module\Director\Objects\IcingaObject; +use InvalidArgumentException; +use stdClass; class BasketSnapshotFieldResolver { @@ -39,7 +42,7 @@ class BasketSnapshotFieldResolver * @return DirectorDatafield[] * @throws \Icinga\Exception\NotFoundError */ - public function loadCurrentFields(Db $db) + public function loadCurrentFields(Db $db): array { $fields = []; foreach ($this->getRequiredIds() as $id) { @@ -90,6 +93,11 @@ class BasketSnapshotFieldResolver $existingFields[(int) $mapping->datafield_id] = $mapping; } foreach ($object->fields as $field) { + if (! isset($fieldMap[(int) $field->datafield_id])) { + throw new InvalidArgumentException( + 'Basket Snapshot contains invalid field reference: ' . $field->datafield_id + ); + } $id = $fieldMap[(int) $field->datafield_id]; if (isset($existingFields[$id])) { unset($existingFields[$id]); @@ -114,6 +122,8 @@ class BasketSnapshotFieldResolver } /** + * For diff purposes only, gives '(UNKNOWN)' for fields missing in our DB + * * @param object $object * @throws \Icinga\Exception\NotFoundError */ @@ -127,21 +137,34 @@ class BasketSnapshotFieldResolver if (isset($map[$id])) { $field->datafield_id = $map[$id]; } else { - $field->datafield_id = "(NEW)"; + $field->datafield_id = "(UNKNOWN)"; } } } } - /** - * @return int - */ - protected function getNextNewId() + public static function fixOptionalDatalistReference(stdClass $plain, Db $db) + { + if (isset($plain->settings->datalist_uuid)) { + unset($plain->settings->datalist); + return; + } + if (isset($plain->settings->datalist)) { + // Just try to load the list, final import will fail if missing + // No modification in case we do not find the list, + if ($list = DirectorDatalist::loadOptional($plain->settings->datalist, $db)) { + unset($plain->settings->datalist); + $plain->settings->datalist_id = $list->get('id'); + } + } + } + + protected function getNextNewId(): int { return $this->nextNewId++; } - protected function getRequiredIds() + protected function getRequiredIds(): array { if ($this->requiredIds === null) { if (isset($this->objects['Datafield'])) { @@ -169,7 +192,7 @@ class BasketSnapshotFieldResolver * @param $type * @return object[] */ - protected function getObjectsByType($type) + protected function getObjectsByType($type): array { if (isset($this->objects->$type)) { return (array) $this->objects->$type; @@ -182,7 +205,7 @@ class BasketSnapshotFieldResolver * @return DirectorDatafield[] * @throws \Icinga\Exception\NotFoundError */ - protected function getTargetFields() + protected function getTargetFields(): array { if ($this->targetFields === null) { $this->calculateIdMap(); @@ -194,7 +217,7 @@ class BasketSnapshotFieldResolver /** * @throws \Icinga\Exception\NotFoundError */ - protected function getIdMap() + protected function getIdMap(): array { if ($this->idMap === null) { $this->calculateIdMap(); |