2019-09-09 07:18:52 -04:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-13 03:38:01 -04:00
|
|
|
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
|
|
|
|
|
|
2019-11-04 19:07:30 -05:00
|
|
|
namespace Icinga\Module\Icingadb\Common;
|
2019-09-09 07:18:52 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Collection of possible host states.
|
|
|
|
|
*/
|
|
|
|
|
class HostStates
|
|
|
|
|
{
|
2025-10-12 16:45:40 -04:00
|
|
|
public const UP = 0;
|
2019-09-09 07:18:52 -04:00
|
|
|
|
2025-10-12 16:45:40 -04:00
|
|
|
public const DOWN = 1;
|
2019-09-09 07:18:52 -04:00
|
|
|
|
2025-10-12 16:45:40 -04:00
|
|
|
public const PENDING = 99;
|
2019-09-09 07:18:52 -04:00
|
|
|
|
2019-11-22 07:42:12 -05:00
|
|
|
/**
|
|
|
|
|
* Get the integer value of the given textual host state
|
|
|
|
|
*
|
|
|
|
|
* @param string $state
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*
|
|
|
|
|
* @throws \InvalidArgumentException If the given host state is invalid, i.e. not known
|
|
|
|
|
*/
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function int(string $state): int
|
2019-11-22 07:42:12 -05:00
|
|
|
{
|
|
|
|
|
switch (strtolower($state)) {
|
|
|
|
|
case 'up':
|
|
|
|
|
$int = self::UP;
|
|
|
|
|
break;
|
|
|
|
|
case 'down':
|
|
|
|
|
$int = self::DOWN;
|
|
|
|
|
break;
|
|
|
|
|
case 'pending':
|
|
|
|
|
$int = self::PENDING;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid host state %d', $state));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $int;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 07:18:52 -04:00
|
|
|
/**
|
|
|
|
|
* Get the textual representation of the passed host state
|
|
|
|
|
*
|
2021-11-03 11:47:14 -04:00
|
|
|
* @param int|null $state
|
2019-09-09 07:18:52 -04:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*
|
|
|
|
|
* @throws \InvalidArgumentException If the given host state is invalid, i.e. not known
|
|
|
|
|
*/
|
2021-11-03 11:47:14 -04:00
|
|
|
public static function text(int $state = null): string
|
2019-09-09 07:18:52 -04:00
|
|
|
{
|
2020-02-07 07:22:34 -05:00
|
|
|
switch (true) {
|
|
|
|
|
case $state === self::UP:
|
2019-09-09 07:18:52 -04:00
|
|
|
$text = 'up';
|
|
|
|
|
break;
|
2020-02-07 07:22:34 -05:00
|
|
|
case $state === self::DOWN:
|
2019-09-09 07:18:52 -04:00
|
|
|
$text = 'down';
|
|
|
|
|
break;
|
2020-02-07 07:22:34 -05:00
|
|
|
case $state === self::PENDING:
|
2019-09-09 07:18:52 -04:00
|
|
|
$text = 'pending';
|
|
|
|
|
break;
|
2020-02-07 07:22:34 -05:00
|
|
|
case $state === null:
|
|
|
|
|
$text = 'not-available';
|
|
|
|
|
break;
|
2019-09-09 07:18:52 -04:00
|
|
|
default:
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid host state %d', $state));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the translated textual representation of the passed host state
|
|
|
|
|
*
|
2021-11-03 11:47:14 -04:00
|
|
|
* @param int|null $state
|
2019-09-09 07:18:52 -04:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*
|
|
|
|
|
* @throws \InvalidArgumentException If the given host state is invalid, i.e. not known
|
|
|
|
|
*/
|
2021-11-03 11:47:14 -04:00
|
|
|
public static function translated(int $state = null): string
|
2019-09-09 07:18:52 -04:00
|
|
|
{
|
2020-02-07 07:22:34 -05:00
|
|
|
switch (true) {
|
|
|
|
|
case $state === self::UP:
|
2020-04-17 12:41:33 -04:00
|
|
|
$text = t('up');
|
2019-09-09 07:18:52 -04:00
|
|
|
break;
|
2020-02-07 07:22:34 -05:00
|
|
|
case $state === self::DOWN:
|
2020-04-17 12:41:33 -04:00
|
|
|
$text = t('down');
|
2019-09-09 07:18:52 -04:00
|
|
|
break;
|
2020-02-07 07:22:34 -05:00
|
|
|
case $state === self::PENDING:
|
2020-04-17 12:41:33 -04:00
|
|
|
$text = t('pending');
|
2019-09-09 07:18:52 -04:00
|
|
|
break;
|
2020-02-07 07:22:34 -05:00
|
|
|
case $state === null:
|
2020-04-17 12:41:33 -04:00
|
|
|
$text = t('not available');
|
2020-02-07 07:22:34 -05:00
|
|
|
break;
|
2019-09-09 07:18:52 -04:00
|
|
|
default:
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid host state %d', $state));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
}
|