1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
<?php
namespace Icinga\Module\Businessprocess\Modification;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\Node;
/**
* NodeRemoveAction
*
* Tracks removed nodes
*
* @package Icinga\Module\Businessprocess
*/
class NodeRemoveAction extends NodeAction
{
protected $preserveProperties = array('parentName');
protected $parentName;
/**
* @param $parentName
* @return $this
*/
public function setParentName($parentName = null)
{
$this->parentName = $parentName;
return $this;
}
/**
* @return mixed
*/
public function getParentName()
{
return $this->parentName;
}
/**
* @inheritdoc
*/
public function appliesTo(BpConfig $config)
{
$name = $this->getNodeName();
$parent = $this->getParentName();
if ($parent === null) {
if (!$config->hasNode($name)) {
$this->error('Toplevel process "%s" not found', $name);
}
} else {
if (! $config->hasNode($parent)) {
$this->error('Parent process "%s" missing', $parent);
} elseif (! $config->getBpNode($parent)->hasChild($name)) {
$this->error('Node "%s" not found in process "%s"', $name, $parent);
}
}
return true;
}
/**
* @inheritdoc
*/
public function applyTo(BpConfig $config)
{
$name = $this->getNodeName();
$parentName = $this->getParentName();
$node = $config->getNode($name);
/** @var ?BpNode $parentBpNode */
$parentBpNode = $parentName ? $config->getNode($parentName) : null;
$this->updateStateOverrides($node, $parentBpNode);
if ($parentName === null) {
if (! $config->hasBpNode($name)) {
$config->removeNode($name);
} else {
$oldDisplay = $config->getBpNode($name)->getDisplay();
$config->removeNode($name);
if ($config->getMetadata()->isManuallyOrdered()) {
foreach ($config->getRootNodes() as $_ => $node) {
$nodeDisplay = $node->getDisplay();
if ($nodeDisplay > $oldDisplay) {
$node->setDisplay($node->getDisplay() - 1);
} elseif ($nodeDisplay === $oldDisplay) {
break; // Stop immediately to not make things worse ;)
}
}
}
}
} else {
$parent = $config->getBpNode($parentName);
$parent->removeChild($name);
$node->removeParent($parentName);
if (! $node->hasParents()) {
$config->removeNode($name);
}
}
}
/**
* Update state overrides
*
* @param Node $node
* @param BpNode|null $nodeParent
*
* @return void
*/
private function updateStateOverrides(Node $node, ?BpNode $nodeParent): void
{
$parents = [];
if ($nodeParent !== null) {
$parents = [$nodeParent];
} else {
$parents = $node->getParents();
}
foreach ($parents as $parent) {
$parentStateOverrides = $parent->getStateOverrides();
unset($parentStateOverrides[$node->getName()]);
$parent->setStateOverrides($parentStateOverrides);
}
}
}
|