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

122 lines
2.6 KiB
PHP
Raw Normal View History

2015-03-16 04:08:00 -04:00
<?php
namespace Icinga\Module\Businessprocess\Modification;
2017-01-11 08:04:45 -05:00
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Node;
2015-03-16 04:08:00 -04:00
class NodeModifyAction extends NodeAction
{
protected $properties = array();
protected $formerProperties = array();
protected $preserveProperties = array('formerProperties', 'properties');
/**
* Set properties for a specific node
*
* Can be called multiple times
*
* @param Node $node
* @param array $properties
*
* @return $this
*/
public function setNodeProperties(Node $node, array $properties)
2015-03-16 04:08:00 -04:00
{
foreach (array_keys($properties) as $key) {
$this->properties[$key] = $properties[$key];
if (array_key_exists($key, $this->formerProperties)) {
continue;
}
$func = 'get' . ucfirst($key);
$this->formerProperties[$key] = $node->$func();
}
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();
2017-01-11 11:36:32 -05:00
if (! $config->hasNode($name)) {
$this->error('Node "%s" not found', $name);
2015-03-16 04:08:00 -04:00
}
2017-01-11 11:36:32 -05:00
$node = $config->getNode($name);
2015-03-16 04:08:00 -04:00
foreach ($this->properties as $key => $val) {
$currentVal = $node->{'get' . ucfirst($key)}();
if ($this->formerProperties[$key] !== $currentVal) {
$this->error(
'Property %s of node "%s" changed its value from "%s" to "%s"',
$key,
$name,
$this->formerProperties[$key],
$currentVal
);
2015-03-16 04:08:00 -04:00
}
}
return true;
}
/**
* @inheritdoc
*/
2017-01-11 11:36:32 -05:00
public function applyTo(BpConfig $config)
2015-03-16 04:08:00 -04:00
{
2017-01-11 11:36:32 -05:00
$node = $config->getNode($this->getNodeName());
2015-03-16 04:08:00 -04:00
foreach ($this->properties as $key => $val) {
$func = 'set' . ucfirst($key);
$node->$func($val);
}
return $this;
}
/**
* @param $properties
* @return $this
*/
2015-03-16 04:08:00 -04:00
public function setProperties($properties)
{
$this->properties = $properties;
return $this;
}
/**
* @param $properties
* @return $this
*/
2015-03-16 04:08:00 -04:00
public function setFormerProperties($properties)
{
$this->formerProperties = $properties;
return $this;
}
/**
* @return array
*/
2015-03-16 04:08:00 -04:00
public function getProperties()
{
return $this->properties;
}
/**
* @return array
*/
2015-03-16 04:08:00 -04:00
public function getFormerProperties()
{
return $this->formerProperties;
}
}