Skip to content

Commit

Permalink
Throw exception when no item is found by getItemByTypeName()
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Feb 10, 2025
1 parent a8e8bdb commit d1594f6
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions tests/src/autoload/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,23 +739,26 @@ function loadDataset()
} else if ($k == 'items_id' && isset($input['itemtype']) && isset($ids[$input['itemtype']][$v]) && !is_numeric($v)) {
$input[$k] = $ids[$input['itemtype']][$v];
} else if ($foreigntype && $foreigntype != 'UNKNOWN' && !is_numeric($v)) {
// not found in ids array, then must get it from DB
if ($obj = getItemByTypeName($foreigntype, $v)) {
$input[$k] = $obj->getID();
}
// not found in ids array, then must get it from DB
$foreign_id = getItemByTypeName($foreigntype, $v, true);
$input[$k] = $foreign_id;

$ids[$foreigntype][$v] = $foreign_id; // cache ID
}
}

if (isset($input['name']) && $item = getItemByTypeName($type, $input['name'])) {
$input['id'] = $ids[$type][$input['name']] = $item->getField('id');
$item->update($input);
$item = getItemForItemtype($type);
$name_field = $item::getNameField();

if (isset($input[$name_field]) && $item->getFromDBByCrit([$name_field => $input[$name_field]])) {
// Update existing item
$item->update([$item::getIndexName() => $item->getID()] + $input);
} else {
// Not found, create it
$item = getItemForItemtype($type);
$id = $item->add($input);
if (isset($input['name'])) {
$ids[$type][$input['name']] = $id;
}
$item->add($input);
}
if (isset($input[$name_field])) {
$ids[$type][$input[$name_field]] = $item->getID(); // cache ID
}
}
}
Expand All @@ -774,17 +777,17 @@ function loadDataset()
/**
* Test helper, search an item from its type and name
*
* @param string $type
* @param string $name
* @param boolean $onlyid
* @return CommonDBTM|false the item, or its id
* @param string $type
* @param string $name
* @param bool $onlyid
* @return CommonDBTM|int the item, or its id
*/
function getItemByTypeName($type, $name, $onlyid = false)
function getItemByTypeName(string $type, string $name, bool $onlyid = false): CommonDBTM|int
{
$item = getItemForItemtype($type);
$nameField = $type::getNameField();
if ($item->getFromDBByCrit([$nameField => $name])) {
return ($onlyid ? $item->getField('id') : $item);
if (!$item->getFromDBByCrit([$nameField => $name])) {
throw new \RuntimeException(sprintf('Unable to load the `%s` item with the name `%s`.', $type, $name));
}
return false;
return ($onlyid ? $item->getID() : $item);
}

0 comments on commit d1594f6

Please sign in to comment.