icingaweb2-module-businessp.../library/Businessprocess/Modification/NodeAddChildrenAction.php

76 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Businessprocess\Modification;
2017-01-11 08:04:45 -05:00
use Icinga\Module\Businessprocess\BpConfig;
class NodeAddChildrenAction extends NodeAction
{
protected $children = array();
protected $preserveProperties = array('children');
/**
* @inheritdoc
*/
2017-01-11 11:36:32 -05:00
public function appliesTo(BpConfig $config)
{
$name = $this->getNodeName();
if (! $config->hasBpNode($name)) {
$this->error('Process "%s" not found', $name);
}
return true;
}
/**
* @inheritdoc
*/
2017-01-11 11:36:32 -05:00
public function applyTo(BpConfig $config)
{
$node = $config->getBpNode($this->getNodeName());
foreach ($this->children as $name) {
if (! $config->hasNode($name) || $config->getNode($name)->getBpConfig()->getName() !== $config->getName()) {
if (strpos($name, ';') !== false) {
list($host, $service) = preg_split('/;/', $name, 2);
if ($service === 'Hoststatus') {
$config->createHost($host);
} else {
$config->createService($host, $service);
}
} elseif ($name[0] === '@' && strpos($name, ':') !== false) {
list($configName, $nodeName) = preg_split('~:\s*~', substr($name, 1), 2);
$config->createImportedNode($configName, $nodeName);
}
}
$node->addChild($config->getNode($name));
}
return $this;
}
/**
* @param array|string $children
* @return $this
*/
public function setChildren($children)
{
if (is_string($children)) {
$children = array($children);
}
$this->children = $children;
return $this;
}
/**
* @return array
*/
public function getChildren()
{
return $this->children;
}
}