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

130 lines
2.9 KiB
PHP
Raw Normal View History

2015-03-16 04:08:00 -04:00
<?php
namespace Icinga\Module\Businessprocess\Modification;
2015-03-16 04:08:00 -04:00
2017-01-11 08:04:45 -05:00
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\Node;
2015-03-16 04:08:00 -04:00
class NodeCreateAction extends NodeAction
{
/** @var string */
2015-03-16 04:08:00 -04:00
protected $parentName;
/** @var array */
2015-03-16 04:08:00 -04:00
protected $properties = array();
/** @var array */
2015-03-16 04:08:00 -04:00
protected $preserveProperties = array('parentName', 'properties');
/**
* @param Node $name
*/
2015-03-16 04:08:00 -04:00
public function setParent(Node $name)
{
2019-02-21 05:32:32 -05:00
$this->parentName = $name->getName();
2015-03-16 04:08:00 -04:00
}
/**
* @return bool
*/
2015-03-16 04:08:00 -04:00
public function hasParent()
{
return $this->parentName !== null;
}
/**
* @return string
*/
2015-03-16 04:08:00 -04:00
public function getParentName()
{
return $this->parentName;
}
/**
* @param string $name
*/
2015-03-16 04:08:00 -04:00
public function setParentName($name)
{
$this->parentName = $name;
}
/**
* @return array
*/
2015-03-16 04:08:00 -04:00
public function getProperties()
{
return $this->properties;
}
/**
* @param array $properties
* @return $this
*/
public function setProperties($properties)
2015-03-16 04:08:00 -04:00
{
$this->properties = (array) $properties;
2015-03-16 04:08:00 -04:00
return $this;
}
/**
* @inheritdoc
*/
2017-01-11 11:36:32 -05:00
public function appliesTo(BpConfig $config)
2015-03-16 04:08:00 -04:00
{
$name = $this->getNodeName();
if ($config->hasNode($name)) {
$this->error('A node with name "%s" already exists', $name);
}
$parent = $this->getParentName();
if ($parent !== null && !$config->hasBpNode($parent)) {
$this->error('Parent process "%s" missing', $parent);
}
return true;
2015-03-16 04:08:00 -04:00
}
/**
* @inheritdoc
*/
2017-01-11 11:36:32 -05:00
public function applyTo(BpConfig $config)
2015-03-16 04:08:00 -04:00
{
$name = $this->getNodeName();
$properties = array(
'name' => $name,
'operator' => $this->properties['operator'],
);
if (array_key_exists('childNames', $this->properties)) {
$properties['child_names'] = $this->properties['childNames'];
} else {
$properties['child_names'] = array();
}
$node = new BpNode((object) $properties);
$node->setBpConfig($config);
2015-03-16 04:08:00 -04:00
foreach ($this->getProperties() as $key => $val) {
if ($key === 'parentName') {
$config->getBpNode($val)->addChild($node);
continue;
}
2015-03-16 04:08:00 -04:00
$func = 'set' . ucfirst($key);
$node->$func($val);
}
if ($node->getDisplay() > 1) {
$i = $node->getDisplay();
foreach ($config->getRootNodes() as $_ => $rootNode) {
if ($rootNode->getDisplay() >= $node->getDisplay()) {
$rootNode->setDisplay(++$i);
}
}
}
$config->addNode($name, $node);
2015-03-16 04:08:00 -04:00
return $node;
}
}