2015-03-16 04:08:00 -04:00
|
|
|
<?php
|
|
|
|
|
|
2016-11-23 06:27:55 -05:00
|
|
|
namespace Icinga\Module\Businessprocess\Modification;
|
2015-03-16 04:08:00 -04:00
|
|
|
|
2016-11-23 06:27:55 -05:00
|
|
|
use Icinga\Module\Businessprocess\BusinessProcess;
|
|
|
|
|
use Icinga\Module\Businessprocess\Node;
|
2015-03-16 04:08:00 -04:00
|
|
|
use Icinga\Web\Session\SessionNamespace as Session;
|
|
|
|
|
|
|
|
|
|
class ProcessChanges
|
|
|
|
|
{
|
2016-11-22 15:11:26 -05:00
|
|
|
/** @var NodeAction[] */
|
2015-03-16 04:08:00 -04:00
|
|
|
protected $changes = array();
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/** @var Session */
|
2015-03-16 04:08:00 -04:00
|
|
|
protected $session;
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/** @var bool */
|
2015-03-16 04:08:00 -04:00
|
|
|
protected $hasBeenModified = false;
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/** @var string Session storage key for this processes changes */
|
2015-03-16 04:08:00 -04:00
|
|
|
protected $sessionKey;
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* ProcessChanges constructor.
|
|
|
|
|
*
|
|
|
|
|
* Direct access is not allowed
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
private function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* @param BusinessProcess $bp
|
|
|
|
|
* @param Session $session
|
|
|
|
|
*
|
|
|
|
|
* @return ProcessChanges
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public static function construct(BusinessProcess $bp, Session $session)
|
|
|
|
|
{
|
|
|
|
|
$key = 'changes.' . $bp->getName();
|
|
|
|
|
$changes = new ProcessChanges();
|
|
|
|
|
$changes->sessionKey = $key;
|
|
|
|
|
|
|
|
|
|
if ($actions = $session->get($key)) {
|
|
|
|
|
foreach ($actions as $string) {
|
2016-11-23 06:27:55 -05:00
|
|
|
$changes->push(NodeAction::unSerialize($string));
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$changes->session = $session;
|
|
|
|
|
|
|
|
|
|
return $changes;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param $properties
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function modifyNode(Node $node, $properties)
|
|
|
|
|
{
|
|
|
|
|
$action = new NodeModifyAction($node);
|
|
|
|
|
$action->setNodeProperties($node, $properties);
|
|
|
|
|
return $this->push($action);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 09:32:56 -05:00
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param $properties
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2016-12-07 17:11:15 -05:00
|
|
|
public function addChildrenToNode($children, Node $node = null)
|
2016-11-29 09:32:56 -05:00
|
|
|
{
|
|
|
|
|
$action = new NodeAddChildrenAction($node);
|
2016-12-07 17:11:15 -05:00
|
|
|
$action->setChildren($children);
|
2016-11-29 09:32:56 -05:00
|
|
|
return $this->push($action);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* @param Node|string $nodeName
|
2016-12-23 05:16:37 -05:00
|
|
|
* @param array $properties
|
2016-11-22 15:11:26 -05:00
|
|
|
* @param Node $parent
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function createNode($nodeName, $properties, Node $parent = null)
|
|
|
|
|
{
|
|
|
|
|
$action = new NodeCreateAction($nodeName);
|
|
|
|
|
$action->setProperties($properties);
|
|
|
|
|
if ($parent !== null) {
|
|
|
|
|
$action->setParent($parent);
|
|
|
|
|
}
|
|
|
|
|
return $this->push($action);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2016-12-16 13:32:36 -05:00
|
|
|
public function deleteNode(Node $node, array $path)
|
2015-03-16 04:08:00 -04:00
|
|
|
{
|
2016-12-16 13:32:36 -05:00
|
|
|
$action = new NodeRemoveAction($node);
|
2016-12-23 05:16:37 -05:00
|
|
|
$action->setPath($path);
|
2016-12-16 13:32:36 -05:00
|
|
|
return $this->push($action);
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Add a new action to the stack
|
|
|
|
|
*
|
|
|
|
|
* @param NodeAction $change
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function push(NodeAction $change)
|
|
|
|
|
{
|
|
|
|
|
$this->changes[] = $change;
|
|
|
|
|
$this->hasBeenModified = true;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Get all stacked actions
|
|
|
|
|
*
|
|
|
|
|
* @return NodeAction[]
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function getChanges()
|
|
|
|
|
{
|
|
|
|
|
return $this->changes;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Forget all changes and remove them from the Session
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function clear()
|
|
|
|
|
{
|
|
|
|
|
$this->hasBeenModified = true;
|
|
|
|
|
$this->changes = array();
|
|
|
|
|
$this->session->set($this->getSessionKey(), null);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Whether there are no stacked changes
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function isEmpty()
|
|
|
|
|
{
|
|
|
|
|
return $this->count() === 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Number of stacked changes
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function count()
|
|
|
|
|
{
|
|
|
|
|
return count($this->changes);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Get the first change on the stack, false if empty
|
|
|
|
|
*
|
|
|
|
|
* @return NodeAction|boolean
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function shift()
|
|
|
|
|
{
|
|
|
|
|
if ($this->isEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->hasBeenModified = true;
|
|
|
|
|
return array_shift($this->changes);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Get the last change on the stack, false if empty
|
|
|
|
|
*
|
|
|
|
|
* @return NodeAction|boolean
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function pop()
|
|
|
|
|
{
|
|
|
|
|
if ($this->isEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->hasBeenModified = true;
|
|
|
|
|
return array_pop($this->changes);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* The identifier used for this processes changes in our Session storage
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
protected function getSessionKey()
|
|
|
|
|
{
|
|
|
|
|
return $this->sessionKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function hasBeenModified()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasBeenModified;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function serialize()
|
|
|
|
|
{
|
|
|
|
|
$serialized = array();
|
|
|
|
|
foreach ($this->getChanges() as $change) {
|
|
|
|
|
$serialized[] = $change->serialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $serialized;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 15:11:26 -05:00
|
|
|
/**
|
|
|
|
|
* Persist to session on destruction
|
|
|
|
|
*/
|
2015-03-16 04:08:00 -04:00
|
|
|
public function __destruct()
|
|
|
|
|
{
|
|
|
|
|
if (! $this->hasBeenModified()) {
|
|
|
|
|
unset($this->session);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$session = $this->session;
|
|
|
|
|
$key = $this->getSessionKey();
|
|
|
|
|
if (! $this->isEmpty()) {
|
|
|
|
|
$session->set($key, $this->serialize());
|
|
|
|
|
}
|
|
|
|
|
unset($this->session);
|
|
|
|
|
}
|
|
|
|
|
}
|