Let all config forms extend BpConfigBaseForm

They're all too similar.
This commit is contained in:
Johannes Meyer 2023-07-25 12:58:16 +02:00
parent 654ba0474d
commit 1ac87cb4ea
9 changed files with 77 additions and 215 deletions

View file

@ -510,7 +510,7 @@ class ProcessController extends Controller
->setParams($this->getRequest()->getUrl()->getParams());
$this->content()->add(
$this->loadForm('bpConfig')
->setProcessConfig($bp)
->setProcess($bp)
->setStorage($this->storage())
->setSuccessUrl($url)
->handleRequest()

View file

@ -4,31 +4,17 @@ namespace Icinga\Module\Businessprocess\Forms;
use Exception;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Common\EnumList;
use Icinga\Module\Businessprocess\ImportedNode;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\Node;
use Icinga\Module\Businessprocess\Storage\Storage;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Session\SessionNamespace;
use ipl\Sql\Connection as IcingaDbConnection;
class AddNodeForm extends QuickForm
class AddNodeForm extends BpConfigBaseForm
{
use EnumList;
/** @var MonitoringBackend|IcingaDbConnection*/
protected $backend;
/** @var Storage */
protected $storage;
/** @var BpConfig */
protected $bp;
/** @var BpNode */
protected $parent;
@ -36,9 +22,6 @@ class AddNodeForm extends QuickForm
protected $processList = array();
/** @var SessionNamespace */
protected $session;
public function setup()
{
$view = $this->getView();
@ -389,37 +372,6 @@ class AddNodeForm extends QuickForm
}
}
/**
* @param MonitoringBackend|IcingaDbConnection $backend
* @return $this
*/
public function setBackend($backend)
{
$this->backend = $backend;
return $this;
}
/**
* @param Storage $storage
* @return $this
*/
public function setStorage(Storage $storage)
{
$this->storage = $storage;
return $this;
}
/**
* @param BpConfig $process
* @return $this
*/
public function setProcess(BpConfig $process)
{
$this->bp = $process;
$this->setBackend($process->getBackend());
return $this;
}
/**
* @param BpNode|null $node
* @return $this
@ -438,16 +390,6 @@ class AddNodeForm extends QuickForm
return $this->parent !== null;
}
/**
* @param SessionNamespace $session
* @return $this
*/
public function setSession(SessionNamespace $session)
{
$this->session = $session;
return $this;
}
protected function hasProcesses()
{
return count($this->enumProcesses()) > 0;

View file

@ -116,12 +116,12 @@ class BpConfigForm extends BpConfigBaseForm
),
));
if ($this->config === null) {
if ($this->bp === null) {
$this->setSubmitLabel(
$this->translate('Add')
);
} else {
$config = $this->config;
$config = $this->bp;
$meta = $config->getMetadata();
foreach ($meta->getProperties() as $k => $v) {
@ -156,13 +156,13 @@ class BpConfigForm extends BpConfigBaseForm
$name = $this->getValue('name');
if ($this->shouldBeDeleted()) {
if ($this->config->isReferenced()) {
if ($this->bp->isReferenced()) {
$this->addError(sprintf(
$this->translate('Process "%s" cannot be deleted as it has been referenced in other processes'),
$name
));
} else {
$this->config->clearAppliedChanges();
$this->bp->clearAppliedChanges();
$this->storage->deleteProcess($name);
$this->setSuccessUrl('businessprocess');
$this->redirectOnSuccess(sprintf('Process %s has been deleted', $name));
@ -174,7 +174,7 @@ class BpConfigForm extends BpConfigBaseForm
{
$name = $this->getValue('name');
if ($this->config === null) {
if ($this->bp === null) {
if ($this->storage->hasProcess($name)) {
$this->addError(sprintf(
$this->translate('A process named "%s" already exists'),
@ -199,7 +199,7 @@ class BpConfigForm extends BpConfigBaseForm
);
$this->setSuccessMessage(sprintf('Process %s has been created', $name));
} else {
$config = $this->config;
$config = $this->bp;
$this->setSuccessMessage(sprintf('Process %s has been stored', $name));
}
$meta = $config->getMetadata();

View file

@ -10,8 +10,6 @@ use Icinga\Web\Notification;
class BpUploadForm extends BpConfigBaseForm
{
protected $backend;
protected $node;
protected $objectList = array();

View file

@ -3,31 +3,18 @@
namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\Node;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Session\SessionNamespace;
use ipl\Sql\Connection as IcingaDbConnection;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
class DeleteNodeForm extends QuickForm
class DeleteNodeForm extends BpConfigBaseForm
{
/** @var MonitoringBackend|IcingaDbConnection */
protected $backend;
/** @var BpConfig */
protected $bp;
/** @var Node */
protected $node;
/** @var BpNode */
protected $parentNode;
/** @var SessionNamespace */
protected $session;
public function setup()
{
$node = $this->node;
@ -80,27 +67,6 @@ class DeleteNodeForm extends QuickForm
));
}
/**
* @param MonitoringBackend|IcingaDbConnection $backend
* @return $this
*/
public function setBackend($backend)
{
$this->backend = $backend;
return $this;
}
/**
* @param BpConfig $process
* @return $this
*/
public function setProcess(BpConfig $process)
{
$this->bp = $process;
$this->setBackend($process->getBackend());
return $this;
}
/**
* @param Node $node
* @return $this
@ -121,16 +87,6 @@ class DeleteNodeForm extends QuickForm
return $this;
}
/**
* @param SessionNamespace $session
* @return $this
*/
public function setSession(SessionNamespace $session)
{
$this->session = $session;
return $this;
}
public function onSuccess()
{
$changes = ProcessChanges::construct($this->bp, $this->session);

View file

@ -3,26 +3,16 @@
namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Common\EnumList;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\Node;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Session\SessionNamespace;
use ipl\Sql\Connection as IcingaDbConnection;
class EditNodeForm extends QuickForm
class EditNodeForm extends BpConfigBaseForm
{
use EnumList;
/** @var MonitoringBackend|IcingaDbConnection */
protected $backend;
/** @var BpConfig */
protected $bp;
/** @var Node */
protected $node;
@ -37,9 +27,6 @@ class EditNodeForm extends QuickForm
protected $host;
/** @var SessionNamespace */
protected $session;
public function setup()
{
$this->host = substr($this->getNode()->getName(), 0, strpos($this->getNode()->getName(), ';'));
@ -283,27 +270,6 @@ class EditNodeForm extends QuickForm
));
}
/**
* @param MonitoringBackend|IcingaDbConnection $backend
* @return $this
*/
public function setBackend($backend)
{
$this->backend = $backend;
return $this;
}
/**
* @param BpConfig $process
* @return $this
*/
public function setProcess(BpConfig $process)
{
$this->bp = $process;
$this->setBackend($process->getBackend());
return $this;
}
/**
* @param BpNode|null $node
* @return $this
@ -322,16 +288,6 @@ class EditNodeForm extends QuickForm
return $this->parent !== null;
}
/**
* @param SessionNamespace $session
* @return $this
*/
public function setSession(SessionNamespace $session)
{
$this->session = $session;
return $this;
}
protected function hasProcesses()
{
return count($this->enumProcesses()) > 0;

View file

@ -3,29 +3,16 @@
namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\Node;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
use Icinga\Web\Notification;
use Icinga\Web\Session\SessionNamespace;
use ipl\Sql\Connection as IcingaDbConnection;
class ProcessForm extends QuickForm
class ProcessForm extends BpConfigBaseForm
{
/** @var MonitoringBackend|IcingaDbConnection */
protected $backend;
/** @var BpConfig */
protected $bp;
/** @var BpNode */
protected $node;
/** @var SessionNamespace */
protected $session;
public function setup()
{
if ($this->node !== null) {
@ -94,27 +81,6 @@ class ProcessForm extends QuickForm
}
}
/**
* @param MonitoringBackend|IcingaDbConnection $backend
* @return $this
*/
public function setBackend($backend)
{
$this->backend = $backend;
return $this;
}
/**
* @param BpConfig $process
* @return $this
*/
public function setProcess(BpConfig $process)
{
$this->bp = $process;
$this->setBackend($process->getBackend());
return $this;
}
/**
* @param BpNode $node
* @return $this
@ -125,16 +91,6 @@ class ProcessForm extends QuickForm
return $this;
}
/**
* @param SessionNamespace $session
* @return $this
*/
public function setSession(SessionNamespace $session)
{
$this->session = $session;
return $this;
}
public function onSuccess()
{
$changes = ProcessChanges::construct($this->bp, $this->session);

View file

@ -4,9 +4,9 @@ namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Module\Businessprocess\MonitoredNode;
use Icinga\Module\Businessprocess\Simulation;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
class SimulationForm extends QuickForm
class SimulationForm extends BpConfigBaseForm
{
/** @var MonitoredNode */
protected $node;

View file

@ -5,16 +5,25 @@ namespace Icinga\Module\Businessprocess\Web\Form;
use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Storage\Storage;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Session\SessionNamespace;
use ipl\Sql\Connection as IcingaDbConnection;
abstract class BpConfigBaseForm extends QuickForm
{
/** @var LegacyStorage */
/** @var Storage */
protected $storage;
/** @var BpConfig */
protected $config;
protected $bp;
/** @var MonitoringBackend|IcingaDbConnection*/
protected $backend;
/** @var SessionNamespace */
protected $session;
protected function listAvailableBackends()
{
@ -28,15 +37,60 @@ abstract class BpConfigBaseForm extends QuickForm
return $keys;
}
public function setStorage(LegacyStorage $storage)
/**
* Set the storage to use
*
* @param Storage $storage
*
* @return $this
*/
public function setStorage(Storage $storage): self
{
$this->storage = $storage;
return $this;
}
public function setProcessConfig(BpConfig $config)
/**
* Set the config to use
*
* @param BpConfig $config
*
* @return $this
*/
public function setProcess(BpConfig $config): self
{
$this->config = $config;
$this->bp = $config;
$this->setBackend($config->getBackend());
return $this;
}
/**
* Set the backend to use
*
* @param MonitoringBackend|IcingaDbConnection $backend
*
* @return $this
*/
public function setBackend($backend): self
{
$this->backend = $backend;
return $this;
}
/**
* Set the session namespace to use
*
* @param SessionNamespace $session
*
* @return $this
*/
public function setSession(SessionNamespace $session): self
{
$this->session = $session;
return $this;
}