Lines
83.33%
5 / 6
Functions and Methods
80.00%
4 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| LogVerbosityPolicy | 83.33% | 5 / 6 | 80.00% | 4 / 5 | 7.23 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| admitsLevel | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| isTracing | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 6 | |||||
| replaceAppVerbosities | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| findLevelFor | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Logging; | |
| 6 | ||
| 7 | use Stewart\Runtime\Model\AppVerbosity; | |
| 8 | use Stewart\Runtime\Model\LogLevel; | |
| 9 | use Stewart\Runtime\Model\ResourceScope; | |
| 10 | ||
| 11 | final class LogVerbosityPolicy | |
| 12 | { | |
| 13 | /** @param array<string, AppVerbosity> $appVerbosities */ | |
| 14 | public function __construct( | |
| 15 | private readonly LogLevel $defaultLevel, | |
| 16 | private array $appVerbosities = [], | |
| 17 | ) {} | |
| 18 | ||
| 19 | public function admitsLevel(LogLevel $level, ?ResourceScope $scope): bool | |
| 20 | { | |
| 21 | return !$level->isBelow($this->findLevelFor($scope)); | |
| 22 | } | |
| 23 | ||
| 24 | public function isTracing(ResourceScope $scope): bool | |
| 25 | { | |
| 26 | return $scope->appId !== null && ($this->appVerbosities[$scope->appId->value]->trace ?? false); | |
| 27 | } | |
| 28 | ||
| 29 | /** @param array<string, AppVerbosity> $appVerbosities */ | |
| 30 | public function replaceAppVerbosities(array $appVerbosities): void | |
| 31 | { | |
| 32 | $this->appVerbosities = $appVerbosities; | |
| 33 | } | |
| 34 | ||
| 35 | private function findLevelFor(?ResourceScope $scope): LogLevel | |
| 36 | { | |
| 37 | $appId = $scope?->appId; | |
| 38 | ||
| 39 | return $appId === null ? $this->defaultLevel : ($this->appVerbosities[$appId->value]->logLevel ?? $this->defaultLevel); | |
| 40 | } | |
| 41 | } |