1 | <?php |
||
2 | |||
3 | namespace LibreNMS\Alert; |
||
4 | |||
5 | use Illuminate\Support\Str; |
||
6 | use LibreNMS\Config; |
||
7 | use LibreNMS\Enum\AlertState; |
||
8 | use LibreNMS\Interfaces\Alert\Transport as TransportInterface; |
||
9 | |||
10 | abstract class Transport implements TransportInterface |
||
11 | { |
||
12 | protected $config; |
||
13 | |||
14 | /** |
||
15 | * Transport constructor. |
||
16 | * |
||
17 | * @param null $transport_id |
||
18 | */ |
||
19 | public function __construct($transport_id = null) |
||
20 | { |
||
21 | if (! empty($transport_id)) { |
||
22 | $sql = 'SELECT `transport_config` FROM `alert_transports` WHERE `transport_id`=?'; |
||
23 | $this->config = json_decode(dbFetchCell($sql, [$transport_id]), true); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
24 | } |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Helper function to parse free form text box defined in ini style to key value pairs |
||
29 | * |
||
30 | * @param string $input |
||
31 | * @return array |
||
32 | */ |
||
33 | protected function parseUserOptions($input) |
||
34 | { |
||
35 | $options = []; |
||
36 | foreach (explode(PHP_EOL, $input) as $option) { |
||
37 | if (Str::contains($option, '=')) { |
||
38 | [$k,$v] = explode('=', $option, 2); |
||
39 | $options[$k] = trim($v); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | return $options; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Get the hex color string for a particular state |
||
48 | * |
||
49 | * @param int $state State code from alert |
||
50 | * @return string Hex color, default to #337AB7 blue if state unrecognised |
||
51 | */ |
||
52 | public static function getColorForState($state) |
||
53 | { |
||
54 | $colors = [ |
||
55 | AlertState::CLEAR => Config::get('alert_colour.ok'), |
||
56 | AlertState::ACTIVE => Config::get('alert_colour.bad'), |
||
57 | AlertState::ACKNOWLEDGED => Config::get('alert_colour.acknowledged'), |
||
58 | AlertState::WORSE => Config::get('alert_colour.worse'), |
||
59 | AlertState::BETTER => Config::get('alert_colour.better'), |
||
60 | ]; |
||
61 | |||
62 | return isset($colors[$state]) ? $colors[$state] : '#337AB7'; |
||
63 | } |
||
64 | } |
||
65 |