Completed
Push — master ( 29a8a8...41c355 )
by Stéphane
03:46
created

RunAction::status()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Todaymade\Daux\Console;
2
3
use Symfony\Component\Console\Output\OutputInterface;
4
use Todaymade\Daux\Daux;
5
6
trait RunAction
7
{
8
    protected function getLength($content) {
9
        return function_exists('mb_strlen') ? mb_strlen($content) : strlen($content);
10
    }
11
12
    protected function runAction($title, $width, \Closure $closure)
13
    {
14
        $verbose = Daux::getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
15
16
        Daux::write($title, $verbose);
17
18
        // 8 is the length of the label + 2 let it breathe
19
        $padding = $width - $this->getLength($title) - 10;
20
21
        try {
22
            $response = $closure(function ($content) use (&$padding) {
23
                $padding -= $this->getLength($content);
24
                Daux::write($content, $verbose);
0 ignored issues
show
Comprehensibility Best Practice introduced by Stéphane Goetz
The variable $verbose seems to be never defined.
Loading history...
25
            });
26
        } catch (\Exception $e) {
27
            $this->status($padding, '[ <fg=red>FAIL</fg=red> ]');
28
            throw $e;
29
        }
30
        $this->status($padding, '[  <fg=green>OK</fg=green>  ]');
31
32
        return $response;
33
    }
34
35
    protected function status($padding, $content)
36
    {
37
        $verbose = Daux::getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
38
        $padding = $verbose ? '' : str_pad(' ', $padding);
39
        Daux::writeln($padding . $content);
40
    }
41
}
42