Skip to content

Commit e99bb0d

Browse files
sukhwinder33445lippserd
authored andcommitted
BpConfig: Add method applyDbStates() to clean-up redundant code
1 parent be8119f commit e99bb0d

9 files changed

Lines changed: 26 additions & 101 deletions

File tree

application/clicommands/CleanupCommand.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77

88
use Exception;
99
use Icinga\Application\Logger;
10-
use Icinga\Application\Modules\Module;
1110
use Icinga\Cli\Command;
1211
use Icinga\Module\Businessprocess\Modification\NodeRemoveAction;
13-
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
14-
use Icinga\Module\Businessprocess\State\IcingaDbState;
15-
use Icinga\Module\Businessprocess\State\MonitoringState;
1612
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
1713

1814
class CleanupCommand extends Command
@@ -62,14 +58,7 @@ public function cleanupAction(): void
6258
continue;
6359
}
6460

65-
if (
66-
Module::exists('icingadb')
67-
&& (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
68-
) {
69-
IcingaDbState::apply($bp);
70-
} else {
71-
MonitoringState::apply($bp);
72-
}
61+
$bp->applyDbStates();
7362

7463
$removedNodes = [];
7564
foreach (array_keys($bp->getMissingChildren()) as $missingNode) {

application/clicommands/ProcessCommand.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77

88
use Exception;
99
use Icinga\Application\Logger;
10-
use Icinga\Application\Modules\Module;
1110
use Icinga\Cli\Command;
1211
use Icinga\Module\Businessprocess\BpConfig;
1312
use Icinga\Module\Businessprocess\BpNode;
1413
use Icinga\Module\Businessprocess\HostNode;
1514
use Icinga\Module\Businessprocess\Node;
16-
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
1715
use Icinga\Module\Businessprocess\ServiceNode;
18-
use Icinga\Module\Businessprocess\State\IcingaDbState;
19-
use Icinga\Module\Businessprocess\State\MonitoringState;
2016
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
2117

2218
class ProcessCommand extends Command
@@ -139,14 +135,7 @@ public function checkAction()
139135
try {
140136
/** @var BpNode $node */
141137
$node = $bp->getNode($nodeName);
142-
if (
143-
Module::exists('icingadb')
144-
&& (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
145-
) {
146-
IcingaDbState::apply($bp);
147-
} else {
148-
MonitoringState::apply($bp);
149-
}
138+
$bp->applyDbStates();
150139

151140
if ($bp->hasErrors()) {
152141
Logger::error("Checking Business Process '%s' failed: %s\n", $name, $bp->getErrors());

application/controllers/NodeController.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66
namespace Icinga\Module\Businessprocess\Controllers;
77

88
use Exception;
9-
use Icinga\Application\Modules\Module;
10-
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
119
use Icinga\Module\Businessprocess\Renderer\Breadcrumb;
1210
use Icinga\Module\Businessprocess\Renderer\TileRenderer;
1311
use Icinga\Module\Businessprocess\Simulation;
14-
use Icinga\Module\Businessprocess\State\IcingaDbState;
15-
use Icinga\Module\Businessprocess\State\MonitoringState;
1612
use Icinga\Module\Businessprocess\Web\Controller;
1713
use Icinga\Module\Businessprocess\Web\Url;
1814
use ipl\Html\Html;
@@ -98,14 +94,7 @@ public function impactAction()
9894
continue;
9995
}
10096

101-
if (
102-
Module::exists('icingadb') &&
103-
(! $config->getBackendName() && IcingadbSupport::useIcingaDbAsBackend())
104-
) {
105-
IcingaDbState::apply($config);
106-
} else {
107-
MonitoringState::apply($config);
108-
}
97+
$config->applyDbStates();
10998
$config->applySimulation($simulation);
11099

111100
foreach ($parents as $parentAndPath) {

application/controllers/ProcessController.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55

66
namespace Icinga\Module\Businessprocess\Controllers;
77

8-
use Icinga\Application\Modules\Module;
98
use Icinga\Date\DateFormatter;
109
use Icinga\Module\Businessprocess\BpConfig;
1110
use Icinga\Module\Businessprocess\BpNode;
1211
use Icinga\Module\Businessprocess\Forms\AddNodeForm;
1312
use Icinga\Module\Businessprocess\Forms\EditNodeForm;
1413
use Icinga\Module\Businessprocess\Node;
15-
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
1614
use Icinga\Module\Businessprocess\Renderer\Breadcrumb;
1715
use Icinga\Module\Businessprocess\Renderer\Renderer;
1816
use Icinga\Module\Businessprocess\Renderer\TileRenderer;
1917
use Icinga\Module\Businessprocess\Renderer\TreeRenderer;
2018
use Icinga\Module\Businessprocess\Simulation;
21-
use Icinga\Module\Businessprocess\State\IcingaDbState;
22-
use Icinga\Module\Businessprocess\State\MonitoringState;
2319
use Icinga\Module\Businessprocess\Storage\ConfigDiff;
2420
use Icinga\Module\Businessprocess\Storage\LegacyConfigRenderer;
2521
use Icinga\Module\Businessprocess\Web\Component\ActionBar;
@@ -97,14 +93,7 @@ public function showAction()
9793
$bp = $this->loadModifiedBpConfig();
9894
$node = $this->getNode($bp);
9995

100-
if (
101-
Module::exists('icingadb') &&
102-
(! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
103-
) {
104-
IcingaDbState::apply($bp);
105-
} else {
106-
MonitoringState::apply($bp);
107-
}
96+
$bp->applyDbStates();
10897

10998
$this->handleSimulations($bp);
11099

library/Businessprocess/BpConfig.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ public function getMetadata()
157157
return $this->metadata;
158158
}
159159

160+
/**
161+
* Apply database states
162+
*
163+
* @return void
164+
*/
165+
public function applyDbStates(): void
166+
{
167+
if (
168+
Module::exists('icingadb')
169+
&& (! $this->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
170+
) {
171+
IcingaDbState::apply($this);
172+
} else {
173+
MonitoringState::apply($this);
174+
}
175+
}
176+
160177
/**
161178
* Set metadata
162179
*
@@ -179,14 +196,7 @@ public function setMetadata(Metadata $metadata)
179196
*/
180197
public function applyChanges(ProcessChanges $changes)
181198
{
182-
if (
183-
Module::exists('icingadb')
184-
&& (! $this->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
185-
) {
186-
IcingaDbState::apply($this);
187-
} else {
188-
MonitoringState::apply($this);
189-
}
199+
$this->applyDbStates();
190200

191201
$cnt = 0;
192202
foreach ($changes->getChanges() as $change) {

library/Businessprocess/Web/Component/Dashboard.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
namespace Icinga\Module\Businessprocess\Web\Component;
77

88
use Exception;
9-
use Icinga\Application\Modules\Module;
109
use Icinga\Authentication\Auth;
1110
use Icinga\Module\Businessprocess\BpConfig;
12-
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
13-
use Icinga\Module\Businessprocess\State\IcingaDbState;
14-
use Icinga\Module\Businessprocess\State\MonitoringState;
1511
use Icinga\Module\Businessprocess\Storage\Storage;
1612
use ipl\Html\BaseHtmlElement;
1713
use ipl\Html\Html;
@@ -112,14 +108,7 @@ protected function __construct(Auth $auth, Storage $storage)
112108
continue;
113109
}
114110

115-
if (
116-
Module::exists('icingadb') &&
117-
(! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
118-
) {
119-
IcingaDbState::apply($bp);
120-
} else {
121-
MonitoringState::apply($bp);
122-
}
111+
$bp->applyDbStates();
123112

124113
$this->add(new BpDashboardTile(
125114
$bp,

library/Businessprocess/Web/Component/DashboardFullscreen.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
namespace Icinga\Module\Businessprocess\Web\Component;
77

88
use Exception;
9-
use Icinga\Application\Modules\Module;
10-
use Icinga\Authentication\Auth;
119
use Icinga\Module\Businessprocess\BpConfig;
12-
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
13-
use Icinga\Module\Businessprocess\State\IcingaDbState;
14-
use Icinga\Module\Businessprocess\State\MonitoringState;
1510
use Icinga\Module\Businessprocess\Storage\Storage;
1611
use ipl\Html\BaseHtmlElement;
1712
use ipl\Html\Html;
@@ -69,14 +64,7 @@ protected function assemble()
6964
continue;
7065
}
7166

72-
if (
73-
Module::exists('icingadb') &&
74-
(! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
75-
) {
76-
IcingaDbState::apply($bp);
77-
} else {
78-
MonitoringState::apply($bp);
79-
}
67+
$bp->applyDbStates();
8068

8169
$this->add(new BpDashboardFullscreenTile(
8270
$bp,

library/Businessprocess/Web/Form/Validator/HostServiceTermValidator.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use Icinga\Module\Businessprocess\BpConfig;
99
use Icinga\Module\Businessprocess\BpNode;
1010
use Icinga\Module\Businessprocess\ServiceNode;
11-
use Icinga\Module\Businessprocess\State\IcingaDbState;
12-
use Icinga\Module\Businessprocess\State\MonitoringState;
13-
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
1411
use ipl\I18n\Translation;
1512
use ipl\Validator\BaseValidator;
1613
use ipl\Web\FormElement\TermInput\Term;
@@ -72,11 +69,7 @@ public function isValid($terms)
7269
}
7370
}
7471

75-
if ($testConfig->getBackend() instanceof MonitoringBackend) {
76-
MonitoringState::apply($testConfig);
77-
} else {
78-
IcingaDbState::apply($testConfig);
79-
}
72+
$testConfig->applyDbStates();
8073

8174
foreach ($terms as $term) {
8275
/** @var Term $term */

library/Businessprocess/Web/Navigation/Renderer/ProcessesProblemsBadge.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
namespace Icinga\Module\Businessprocess\Web\Navigation\Renderer;
77

88
use Icinga\Application\Logger;
9-
use Icinga\Application\Modules\Module;
109
use Icinga\Module\Businessprocess\Node;
11-
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
12-
use Icinga\Module\Businessprocess\State\IcingaDbState;
13-
use Icinga\Module\Businessprocess\State\MonitoringState;
1410
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
1511
use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer;
1612
use Throwable;
@@ -35,14 +31,7 @@ public function getCount()
3531

3632
foreach ($storage->listProcessNames() as $processName) {
3733
$bp = $storage->loadProcess($processName);
38-
if (
39-
Module::exists('icingadb') &&
40-
(! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
41-
) {
42-
IcingaDbState::apply($bp);
43-
} else {
44-
MonitoringState::apply($bp);
45-
}
34+
$bp->applyDbStates();
4635

4736
foreach ($bp->getRootNodes() as $rootNode) {
4837
$nodeState = $rootNode->getState();

0 commit comments

Comments
 (0)