1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Server\Logging;
15:
16: use Nexus\Mcp\Core\Schema\Enum\LoggingLevel;
17:
18: /**
19: * Holds the minimum `LoggingLevel` for server-emitted log notifications.
20: * Updated by `logging/setLevel` and consulted by `ServerContext::log()`.
21: */
22: final class LoggingLevelGate
23: {
24: public function __construct(public private(set) LoggingLevel $level = LoggingLevel::Info)
25: {
26: }
27:
28: public function setLevel(LoggingLevel $level): void
29: {
30: $this->level = $level;
31: }
32:
33: public function shouldEmit(LoggingLevel $messageLevel): bool
34: {
35: return self::resolveSeverityIndex($messageLevel) <= self::resolveSeverityIndex($this->level);
36: }
37:
38: /**
39: * RFC 5424 numeric severity index (0 = most severe, 7 = least).
40: *
41: * @return int<0, 7>
42: */
43: private static function resolveSeverityIndex(LoggingLevel $level): int
44: {
45: return match ($level) {
46: LoggingLevel::Emergency => 0,
47: LoggingLevel::Alert => 1,
48: LoggingLevel::Critical => 2,
49: LoggingLevel::Error => 3,
50: LoggingLevel::Warning => 4,
51: LoggingLevel::Notice => 5,
52: LoggingLevel::Info => 6,
53: LoggingLevel::Debug => 7,
54: };
55: }
56: }
57: