Completed
Push — master ( e5b266...1db263 )
by Michael
02:19
created

Command::echoRaw()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Schnittstabil\Dartisan\Commands;
4
5
use Garden\Cli\Args;
6
7
abstract class Command
8
{
9
    protected $args;
10
    protected $outputFormatter;
11
12
    public function __construct(Args $args, callable $outputFormatter)
13
    {
14
        $this->args = $args;
15
        $this->outputFormatter = $outputFormatter;
16
    }
17
18
    abstract protected function run();
19
20
    public function __invoke()
21
    {
22
        try {
23
            return $this->run();
24
        } catch (\Exception $e) {
25
            $this->echoError($e);
26
27
            return 2;
28
        }
29
    }
30
31
    /**
32
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
33
     */
34
    protected function echoRaw($msg, $eol = true)
35
    {
36
        $outputFormatter = $this->outputFormatter;
37
        $output = $outputFormatter($msg);
38
39
        if ($eol) {
40
            $output .= PHP_EOL;
41
        }
42
43
        echo $output;
44
    }
45
46
    /**
47
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
48
     */
49
    protected function echoError($msg, $eol = true)
50
    {
51
        $this->echoRaw('<error>'.$msg.'</error>', $eol);
52
    }
53
54
    /**
55
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
56
     */
57
    protected function echoInfo($msg, $eol = true)
58
    {
59
        $this->echoRaw('<info>'.$msg.'</info>', $eol);
60
    }
61
62
    protected function echoNotes($notes)
63
    {
64
        foreach ($notes as $note) {
65
            $this->echoRaw($note);
66
        }
67
    }
68
}
69