icingaweb2-module-businessp.../library/Businessprocess/Modification/NodeAddChildrenAction.php
2017-01-11 14:09:30 +01:00

71 lines
1.5 KiB
PHP

<?php
namespace Icinga\Module\Businessprocess\Modification;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
class NodeAddChildrenAction extends NodeAction
{
protected $children = array();
protected $preserveProperties = array('children');
/**
* @inheritdoc
*/
public function appliesTo(BpConfig $bp)
{
$name = $this->getNodeName();
if (! $bp->hasNode($name)) {
return false;
}
return $bp->getNode($name) instanceof BpNode;
}
/**
* @inheritdoc
*/
public function applyTo(BpConfig $bp)
{
/** @var BpNode $node */
if (! $this->hasNode()) {
// TODO: We end up here when defining "top nodes", but that would probably
// be a different action
return $this;
}
$node = $bp->getNode($this->getNodeName());
$existing = $node->getChildNames();
foreach ($this->children as $name) {
if (! in_array($name, $existing)) {
$existing[] = $name;
}
}
$node->setChildNames($existing);
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;
}
}