IcingaArguments: class needed some love

This commit is contained in:
Thomas Gelf 2015-12-02 02:50:41 +01:00
parent 245189c099
commit 8be79b3dd6
1 changed files with 69 additions and 7 deletions

View File

@ -49,7 +49,7 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
return null; return null;
} }
return $this->groups[$this->idx[$this->position]]; return $this->arguments[$this->idx[$this->position]];
} }
public function key() public function key()
@ -76,20 +76,70 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
return null; return null;
} }
public function set($key, $value)
{
// var_dump(sprintf('Setting %s', var_export($value, 1)));
// type => 'Function'
// required => true <-- MISSING
$attrs = array(
'argument_name' => $key,
);
$map = array(
'skip_key' => 'skip_key',
'repeat_key' => 'repeat_key',
'order' => 'sort_order',
'description' => 'description',
'set_if' => 'set_if',
);
$argValue = null;
if (is_object($value)) {
foreach ($map as $apiKey => $dbKey) {
if (property_exists($value, $apiKey)) {
$attrs[$dbKey] = $value->$apiKey;
}
}
if (property_exists($value, 'value')) {
$argValue = $value->value;
}
if (property_exists($value, 'type')) {
if ($value->type === 'Function') {
$attrs['argument_value'] = '/* Unable to fetch function body through API */';
$attrs['argument_format'] = 'expression';
}
}
} else {
$argValue = $value;
}
if (is_string($argValue)) {
$attrs['argument_value'] = $argValue;
$attrs['argument_format'] = 'string';
} elseif ($argValue !== null) {
$attrs['argument_value'] = $argValue;
$attrs['argument_format'] = 'json';
}
$this->add(IcingaCommandArgument::create($attrs));
return $this;
}
/** /**
* Magic isset check * Magic isset check
* *
* @return boolean * @return boolean
*/ */
public function __isset($group) public function __isset($argument)
{ {
return array_key_exists($group, $this->groups); return array_key_exists($argument, $this->arguments);
} }
public function remove($argument) public function remove($argument)
{ {
if (array_key_exists($group, $this->groups)) { if (array_key_exists($argument, $this->arguments)) {
unset($this->groups[$group]); unset($this->arguments[$argument]);
} }
$this->modified = true; $this->modified = true;
@ -98,8 +148,8 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
protected function refreshIndex() protected function refreshIndex()
{ {
ksort($this->groups); ksort($this->arguments);
$this->idx = array_keys($this->groups); $this->idx = array_keys($this->arguments);
} }
public function add(IcingaCommandArgument $argument) public function add(IcingaCommandArgument $argument)
@ -158,6 +208,18 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
return $arguments->loadFromDb(); return $arguments->loadFromDb();
} }
public function store()
{
$db = $this->object->getConnection();
foreach ($this->arguments as $argument) {
$argument->command_id = $this->object->id;
$argument->store($db);
}
return $this;
}
public function toConfigString() public function toConfigString()
{ {
if (empty($this->arguments)) { if (empty($this->arguments)) {