Completed
Push — nln-php7 ( 6680df...1a6b54 )
by Nicolas
02:06
created

OutputAware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 52
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma\Logging;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
trait OutputAware
10
{
11
    protected ?OutputInterface
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
12
        $output = null;
13
14 40
    public function setOutput(OutputInterface $output)
15
    {
16 40
        $this->output = $output;
17
18 40
        return $this;
19
    }
20
21 1
    protected function error($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void
22
    {
23 1
        $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'red');
24 1
    }
25
26 1
    protected function warning($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void
27
    {
28 1
        $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'yellow');
29 1
    }
30
31 1
    protected function info($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void
32
    {
33 1
        $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'white');
34 1
    }
35
36 1
    protected function debug($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL): void
37
    {
38 1
        $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_VERBOSE, 'white');
39 1
    }
40
41 4
    private function write($messages, $newline, $type, $verbosity, $textColor): void
42
    {
43 4
        if($this->output instanceof OutputInterface)
44
        {
45 4
            if($verbosity <= $this->output->getVerbosity())
46
            {
47 4
                if(! is_array($messages))
48
                {
49 4
                    $messages = array($messages);
50
                }
51
52 4
                array_walk($messages, function(& $message) use($textColor) {
53 4
                    $message = "<fg=$textColor>$message</fg=$textColor>";
54 4
                });
55
56 4
                $this->output->write($messages, $newline, $type);
57
            }
58
        }
59 4
    }
60
}
61