CompilerPsrLogger   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getOutput() 0 11 3
A log() 0 15 2
A getVerbosity() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Logger;
16
17
use Psr\Log\AbstractLogger;
18
use Psr\Log\LogLevel;
19
use Stringable;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use function array_key_exists;
22
23
final class CompilerPsrLogger extends AbstractLogger
24
{
25
    public function __construct(
26
        private readonly CompilerLogger $decoratedLogger,
0 ignored issues
show
Bug introduced by
The type KevinGH\Box\Console\Logger\CompilerLogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
    ) {
28
    }
29
30
    public function log($level, string|Stringable $message, array $context = []): void
31
    {
32
        $verbosity = self::getVerbosity($level);
33
        $output = self::getOutput($context);
34
35
        if (null === $output) {
36
            $this->decoratedLogger->log(
37
                CompilerLogger::CHEVRON_PREFIX,
38
                $message,
39
                $verbosity,
40
            );
41
        } else {
42
            $this->decoratedLogger->getIO()->writeln(
43
                $output,
44
                $verbosity,
45
            );
46
        }
47
    }
48
49
    private static function getVerbosity(string $level): int
50
    {
51
        return match ($level) {
52
            LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE,
53
            LogLevel::DEBUG => OutputInterface::VERBOSITY_VERY_VERBOSE,
54
            default => OutputInterface::OUTPUT_NORMAL,
55
        };
56
    }
57
58
    private static function getOutput(array $context): ?string
59
    {
60
        $knownKeys = ['stdout', 'stderr'];
61
62
        foreach ($knownKeys as $knownKey) {
63
            if (array_key_exists($knownKey, $context)) {
64
                return $context[$knownKey];
65
            }
66
        }
67
68
        return null;
69
    }
70
}
71