Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| LogLevel | |
100.00% |
31 / 31 |
|
100.00% |
4 / 4 |
20 | |
100.00% |
1 / 1 |
| description | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
9 | |||
| weight | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
9 | |||
| isAtLeast | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| ordered | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Ergonomic utilities for PHP enums, including names, values, lookups, and option maps. |
| 7 | * |
| 8 | * This file is part of fast-forward/enum project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/enum |
| 14 | * @see https://github.com/php-fast-forward/enum/issues |
| 15 | * @see https://php-fast-forward.github.io/enum/ |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Enum\Logger; |
| 20 | |
| 21 | use FastForward\Enum\DescribedEnumInterface; |
| 22 | use FastForward\Enum\LabeledEnumInterface; |
| 23 | use FastForward\Enum\Trait\Comparable; |
| 24 | use FastForward\Enum\Trait\HasLabel; |
| 25 | use FastForward\Enum\Trait\HasNameLookup; |
| 26 | use FastForward\Enum\Trait\HasNameMap; |
| 27 | use FastForward\Enum\Trait\HasNames; |
| 28 | use FastForward\Enum\Trait\HasOptions; |
| 29 | use FastForward\Enum\Trait\HasValueMap; |
| 30 | use FastForward\Enum\Trait\HasValues; |
| 31 | |
| 32 | enum LogLevel: string implements DescribedEnumInterface, LabeledEnumInterface |
| 33 | { |
| 34 | use Comparable; |
| 35 | use HasLabel; |
| 36 | use HasNameLookup; |
| 37 | use HasNameMap; |
| 38 | use HasNames; |
| 39 | use HasOptions; |
| 40 | use HasValueMap; |
| 41 | use HasValues; |
| 42 | |
| 43 | case Emergency = 'emergency'; |
| 44 | case Alert = 'alert'; |
| 45 | case Critical = 'critical'; |
| 46 | case Error = 'error'; |
| 47 | case Warning = 'warning'; |
| 48 | case Notice = 'notice'; |
| 49 | case Info = 'info'; |
| 50 | case Debug = 'debug'; |
| 51 | |
| 52 | /** |
| 53 | * @return string |
| 54 | */ |
| 55 | public function description(): string |
| 56 | { |
| 57 | return match ($this) { |
| 58 | self::Emergency => 'System is unusable and requires immediate global attention.', |
| 59 | self::Alert => 'Action must be taken immediately to avoid severe service disruption.', |
| 60 | self::Critical => 'Critical condition indicating serious failure in a core capability.', |
| 61 | self::Error => 'Runtime error indicating part of the current operation failed.', |
| 62 | self::Warning => 'Potential issue that should be reviewed before it becomes an error.', |
| 63 | self::Notice => 'Normal but noteworthy event that may deserve operational awareness.', |
| 64 | self::Info => 'Informational event describing expected application flow.', |
| 65 | self::Debug => 'Verbose diagnostic information intended for debugging and development.', |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return int |
| 71 | */ |
| 72 | public function weight(): int |
| 73 | { |
| 74 | return match ($this) { |
| 75 | self::Debug => 10, |
| 76 | self::Info => 20, |
| 77 | self::Notice => 30, |
| 78 | self::Warning => 40, |
| 79 | self::Error => 50, |
| 80 | self::Critical => 60, |
| 81 | self::Alert => 70, |
| 82 | self::Emergency => 80, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param self $level |
| 88 | * |
| 89 | * @return bool |
| 90 | */ |
| 91 | public function isAtLeast(self $level): bool |
| 92 | { |
| 93 | return $this->weight() >= $level->weight(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return list<self> |
| 98 | */ |
| 99 | public static function ordered(): array |
| 100 | { |
| 101 | return [ |
| 102 | self::Debug, |
| 103 | self::Info, |
| 104 | self::Notice, |
| 105 | self::Warning, |
| 106 | self::Error, |
| 107 | self::Critical, |
| 108 | self::Alert, |
| 109 | self::Emergency, |
| 110 | ]; |
| 111 | } |
| 112 | } |