Skip to content

Commit 345f7dd

Browse files
committed
Dry refactor getUserThemeMode
1 parent 5b72c00 commit 345f7dd

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

application/controllers/IcingadbimgController.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function init()
9292
$this->defaultDashboardPanelId
9393
);
9494
$this->defaultOrgId = $this->myConfig->get('defaultorgid', $this->defaultOrgId);
95-
$this->grafanaTheme = $this->getUserThemeMode();
95+
$this->grafanaTheme = Util::getUserThemeMode(Auth::getInstance()->getUser());
9696
$this->height = $this->myConfig->get('height', $this->height);
9797
$this->width = $this->myConfig->get('width', $this->width);
9898
$this->proxyTimeout = $this->myConfig->get('proxytimeout', $this->proxyTimeout);
@@ -354,26 +354,4 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
354354
$imageHtml = $result;
355355
return true;
356356
}
357-
358-
/**
359-
* getUserThemeMode returns the users configured Theme Mode.
360-
* Since we cannot handle the 'system' setting (it's client-side),
361-
* we default to 'dark'.
362-
* @return string
363-
*/
364-
private function getUserThemeMode(): string
365-
{
366-
$mode = 'dark';
367-
368-
if ($user = Auth::getInstance()->getUser()) {
369-
$mode = $user->getPreferences()->getValue('icingaweb', 'theme_mode', $mode);
370-
}
371-
372-
// Could be system, which we cannot handle since it's browser-side
373-
if (!in_array($mode, ['dark', 'light'])) {
374-
$mode = 'dark';
375-
}
376-
377-
return $mode;
378-
}
379357
}

library/Grafana/Helpers/Util.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Icinga\Module\Grafana\Helpers;
44

5+
use Icinga\User;
6+
57
class Util
68
{
79
public static function graphiteReplace(string $string = ''): string
@@ -10,4 +12,28 @@ public static function graphiteReplace(string $string = ''): string
1012

1113
return $string;
1214
}
15+
16+
/**
17+
* getUserThemeMode returns the users configured Theme Mode.
18+
* Since we cannot handle the 'system' setting (it's client-side),
19+
* we default to 'dark'.
20+
*
21+
* @param User $user
22+
* @return string
23+
*/
24+
public static function getUserThemeMode(User $user): string
25+
{
26+
$mode = 'dark';
27+
28+
if (isset($user)) {
29+
$mode = $user->getPreferences()->getValue('icingaweb', 'theme_mode', $mode);
30+
}
31+
32+
// Could be system, which we cannot handle since it's browser-side
33+
if (!in_array($mode, ['dark', 'light'])) {
34+
$mode = 'dark';
35+
}
36+
37+
return $mode;
38+
}
1339
}

library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected function init()
127127
);
128128

129129
$this->defaultOrgId = $this->config->get('defaultorgid', $this->defaultOrgId);
130-
$this->grafanaTheme = $this->getUserThemeMode();
130+
$this->grafanaTheme = Util::getUserThemeMode(Auth::getInstance()->getUser());
131131
$this->height = $this->config->get('height', $this->height);
132132
$this->width = $this->config->get('width', $this->width);
133133

@@ -781,26 +781,4 @@ private function createDebugTable()
781781

782782
return $grafanaTable;
783783
}
784-
785-
/**
786-
* getUserThemeMode returns the users configured Theme Mode.
787-
* Since we cannot handle the 'system' setting (it's client-side),
788-
* we default to 'dark'.
789-
* @return string
790-
*/
791-
private function getUserThemeMode(): string
792-
{
793-
$mode = 'dark';
794-
795-
if ($user = Auth::getInstance()->getUser()) {
796-
$mode = $user->getPreferences()->getValue('icingaweb', 'theme_mode', $mode);
797-
}
798-
799-
// Could be system, which we cannot handle since it's browser-side
800-
if (!in_array($mode, ['dark', 'light'])) {
801-
$mode = 'dark';
802-
}
803-
804-
return $mode;
805-
}
806784
}

0 commit comments

Comments
 (0)