Passed
Push — master ( 67a15a...cb2889 )
by Nicolas
04:52
created

OutputAware::write()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 8
nc 4
nop 5
1
<?php
2
3
namespace Karma\Logging;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
trait OutputAware
8
{
9
    protected
10
        $output = null;
11
12
    public function setOutput(OutputInterface $output)
13
    {
14
        $this->output = $output;
15
16
        return $this;
17
    }
18
19
    protected function error($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
20
    {
21
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'red');
22
    }
23
24
    protected function warning($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
25
    {
26
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'yellow');
27
    }
28
29
    protected function info($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
30
    {
31
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'white');
32
    }
33
34
    protected function debug($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
35
    {
36
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_VERBOSE, 'white');
37
    }
38
39
    private function write($messages, $newline, $type, $verbosity, $textColor)
40
    {
41
        if($this->output instanceof OutputInterface)
42
        {
43
            if($verbosity <= $this->output->getVerbosity())
44
            {
45
                if(! is_array($messages))
46
                {
47
                    $messages = array($messages);
48
                }
49
50
                array_walk($messages, function(& $message) use($textColor) {
51
                    $message = "<fg=$textColor>$message</fg=$textColor>";
52
                });
53
54
                $this->output->write($messages, $newline, $type);
55
            }
56
        }
57
    }
58
}
59