RunAction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 34
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLength() 0 2 2
A status() 0 5 2
A runAction() 0 21 2
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, $verbose) {
23
                $padding -= $this->getLength($content);
24
                Daux::write($content, $verbose);
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