Passed
Push — test ( 3e2008...5a4346 )
by Tom
02:40
created

FileShowerAbstract::textTableGetRow()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility\Show;
6
7
use Ktomk\Pipelines\File\File;
8
9
/**
10
 * Class FileShowerAbstract
11
 *
12
 * Show details about the pipelines file on text output
13
 *
14
 * @package Ktomk\Pipelines\Utility\Show
15
 */
16
class FileShowerAbstract
17
{
18
    /**
19
     * @var callable
20
     */
21
    protected $output;
22
23
    /**
24
     * @var File
25
     */
26
    protected $file;
27
28
    /**
29
     * FileInfo constructor.
30
     *
31
     * @param callable $output
32
     * @param File $file
33
     */
34 16
    public function __construct($output, File $file)
35
    {
36 16
        $this->output = $output;
37 16
        $this->file = $file;
38 16
    }
39
40
    /**
41
     * @param string $message
42
     *
43
     * @return void
44
     */
45 15
    protected function info($message)
46
    {
47 15
        call_user_func($this->output, $message);
48 15
    }
49
50
    /* text table implementation */
51
52
    /**
53
     * @param array $table
54
     *
55
     * @return void
56
     */
57 12
    protected function textTable(array $table)
58
    {
59 12
        $sizes = $this->textTableGetSizes($table);
60
61 12
        foreach ($table as $row) {
62 12
            $line = $this->textTableGetRow($row, $sizes);
63 12
            $this->info($line);
64
        }
65 12
    }
66
67
    /**
68
     * get max sizes for each column in array table
69
     *
70
     * @param array $table
71
     *
72
     * @return array|int[] sizes
73
     */
74 12
    private function textTableGetSizes(array $table)
75
    {
76 12
        $sizes = array();
77 12
        foreach ($table[0] as $index => $cell) {
78 12
            $sizes[$index] = 0;
79
        }
80
81 12
        foreach ($table as $row) {
82 12
            foreach ($row as $index => $column) {
83 12
                $sizes[$index] = max($sizes[$index], strlen($column));
84
            }
85
        }
86
87 12
        return $sizes;
88
    }
89
90
    /**
91
     * @param array|string[] $row
92
     * @param array|int[] $sizes
93
     *
94
     * @return string
95
     */
96 12
    private function textTableGetRow(array $row, array $sizes)
97
    {
98 12
        $line = '';
99 12
        foreach ($row as $index => $column) {
100 12
            $len = strlen($column);
101 12
            $index && $line .= '    ';
102 12
            $line .= $column;
103 12
            $line .= str_repeat(' ', $sizes[$index] - $len);
104
        }
105
106 12
        return $line;
107
    }
108
}
109