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

109 lines
2.2 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)
{
$this->parentName = (string) $name;
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
{
2017-01-11 11:36:32 -05:00
return ! $config->hasNode($this->getNodeName());
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($bp, (object) $properties);
2015-03-16 04:08:00 -04:00
foreach ($this->getProperties() as $key => $val) {
2015-03-16 04:08:00 -04:00
$func = 'set' . ucfirst($key);
$node->$func($val);
}
$bp->addNode($name, $node);
2015-03-16 04:08:00 -04:00
if ($this->hasParent()) {
$node->addParent($bp->getNode($this->getParentName()));
}
return $node;
}
}