Completed
Push — 42-formatter ( cff7a4 )
by Nicolas
32:10 queued 28:41
created

OutputInterfaceAdapter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 8
c 5
b 2
f 0
lcom 1
cbo 2
dl 0
loc 70
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A writeLevel() 0 9 1
A __construct() 0 11 1
A log() 0 8 2
A convertLevel() 0 11 2
A colorizeMessage() 0 18 2
1
<?php
2
3
namespace Karma\Logging;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Psr\Log\LogLevel;
8
9
class OutputInterfaceAdapter implements LoggerInterface
10
{
11
    use \Psr\Log\LoggerTrait;
12
13
    private
14
        $output,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
15
        $levelConversionTable;
16
    
17
    public function __construct(OutputInterface $output)
18
    {
19
        $this->output = $output;
20
        
21
        $this->levelConversionTable = array(
22
            LogLevel::DEBUG => OutputInterface::VERBOSITY_VERBOSE,
23
            LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
24
            LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
25
            LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL
26
        );
27
    }
28
    
29
    public function log($level, $message, array $context = array())
30
    {
31
        if($this->convertLevel($level) <= $this->output->getVerbosity())
32
        {
33
            $this->writeLevel($level);
34
            $this->output->writeln($message, OutputInterface::OUTPUT_NORMAL);
35
        }
36
    }
37
    
38
    private function convertLevel($level)
39
    {
40
        $verbosity = OutputInterface::VERBOSITY_NORMAL;
41
        
42
        if(array_key_exists($level, $this->levelConversionTable))
43
        {
44
            $verbosity = $this->levelConversionTable[$level];
45
        }
46
        
47
        return $verbosity;
48
    }
49
    
50
    private function writeLevel($level)
51
    {
52
        $message = str_pad(sprintf(
53
           '[%s]',
54
           strtoupper($level)
55
        ), 10);
56
57
        $this->output->write($this->colorizeMessage($level, $message));
58
    }
59
    
60
    private function colorizeMessage($level, $message)
61
    {
62
        $colors = array(
63
            LogLevel::ERROR => 'red',
64
            LogLevel::WARNING => 'yellow',
65
        );
66
        
67
        if(isset($colors[$level]))
68
        {
69
            $message = sprintf(
70
               '<%1$s>%2$s</%1$s>',
71
                'fg=' . $colors[$level] . ';options=bold',
72
                $message
73
            );
74
        }
75
        
76
        return $message;
77
    }
78
}