icingadb-web/library/Icingadb/Common/HostStates.php

108 lines
2.7 KiB
PHP
Raw Permalink Normal View History

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
*/
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
{
switch (true) {
case $state === self::UP:
2019-09-09 07:18:52 -04:00
$text = 'up';
break;
case $state === self::DOWN:
2019-09-09 07:18:52 -04:00
$text = 'down';
break;
case $state === self::PENDING:
2019-09-09 07:18:52 -04:00
$text = 'pending';
break;
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
{
switch (true) {
case $state === self::UP:
$text = t('up');
2019-09-09 07:18:52 -04:00
break;
case $state === self::DOWN:
$text = t('down');
2019-09-09 07:18:52 -04:00
break;
case $state === self::PENDING:
$text = t('pending');
2019-09-09 07:18:52 -04:00
break;
case $state === null:
$text = t('not available');
break;
2019-09-09 07:18:52 -04:00
default:
throw new \InvalidArgumentException(sprintf('Invalid host state %d', $state));
}
return $text;
}
}