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

72 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Businessprocess\Modification;
use Icinga\Module\Businessprocess\BpNode;
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 08:04:45 -05:00
public function appliesTo(BpConfig $bp)
{
$name = $this->getNodeName();
if (! $bp->hasNode($name)) {
return false;
}
return $bp->getNode($name) instanceof BpNode;
}
/**
* @inheritdoc
*/
2017-01-11 08:04:45 -05:00
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;
}
}