icingaweb2-module-businessp.../library/Businessprocess/ImportedNode.php

183 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Businessprocess;
use Icinga\Application\Config;
use Icinga\Module\Businessprocess\Html\Link;
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
use Exception;
class ImportedNode extends Node
{
/** @var string */
protected $configName;
/** @var BpNode */
private $node;
protected $className = 'subtree';
/**
* @inheritdoc
*/
public function __construct(BusinessProcess $bp, $object)
{
$this->name = $object->name;
$this->configName = $object->configName;
$this->bp = $bp;
if (isset($object->state)) {
$this->setState($object->state);
} else {
$this->setMissing();
}
}
/**
* @return string
*/
public function getConfigName()
{
return $this->configName;
}
/**
* @inheritdoc
*/
public function getState()
{
if ($this->state === null) {
$this->state = $this->importedNode()->getState();
}
return $this->state;
}
/**
* @inheritdoc
*/
public function getAlias()
{
return $this->importedNode()->getAlias();
}
2016-12-22 08:18:13 -05:00
public function getUrl()
{
$params = array(
'config' => $this->getConfigName(),
2016-12-22 08:19:00 -05:00
'node' => $this->importedNode()->getName()
2016-12-22 08:18:13 -05:00
);
return Url::fromPath('businessprocess/process/show', $params);
}
/**
* @inheritdoc
*/
public function isMissing()
{
2015-03-16 04:08:00 -04:00
// TODO: WHY? return $this->getState() === null;
return $this->importedNode()->isMissing();
}
/**
* @inheritdoc
*/
public function isInDowntime()
{
if ($this->downtime === null) {
$this->downtime = $this->importedNode()->isInDowntime();
}
return $this->downtime;
}
/**
* @inheritdoc
*/
public function isAcknowledged()
{
if ($this->ack === null) {
$this->downtime = $this->importedNode()->isAcknowledged();
}
return $this->ack;
}
/**
* @return BpNode
*/
protected function importedNode()
{
if ($this->node === null) {
$this->node = $this->loadImportedNode();
}
return $this->node;
}
/**
* @return BpNode
*/
protected function loadImportedNode()
{
try {
$import = $this->storage()->loadProcess($this->configName);
if ($this->bp->usesSoftStates()) {
$import->useSoftStates();
} else {
$import->useHardStates();
}
2015-03-16 04:08:00 -04:00
$import->retrieveStatesFromBackend();
2015-03-16 04:08:00 -04:00
return $import->getNode($this->name);
} catch (Exception $e) {
2015-03-16 04:08:00 -04:00
return $this->createFailedNode($e);
}
}
2015-03-16 04:08:00 -04:00
/**
* @return LegacyStorage
*/
protected function storage()
{
return new LegacyStorage(
Config::module('businessprocess')->getSection('global')
);
2015-03-16 04:08:00 -04:00
}
/**
* @param Exception $e
*
* @return BpNode
*/
protected function createFailedNode(Exception $e)
2015-03-16 04:08:00 -04:00
{
$node = new BpNode($this->bp, (object) array(
'name' => $this->name,
'operator' => '&',
'child_names' => array()
2015-03-16 04:08:00 -04:00
));
$node->setState(2);
$node->setMissing(false)
->setDowntime(false)
->setAck(false)
->setAlias($e->getMessage());
return $node;
}
/**
* @inheritdoc
*/
public function getLink()
{
return Link::create(
$this->getAlias(),
'businessprocess/process/show',
array(
'config' => $this->configName,
'process' => $this->name
)
);
}
}