Passed
Push — master ( a5203b...48c4ea )
by Tom
04:36
created

FileShowerAbstract::getShowPipeline()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
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 InvalidArgumentException;
8
use Ktomk\Pipelines\File\File;
9
use Ktomk\Pipelines\File\ParseException;
10
use Ktomk\Pipelines\File\Pipelines;
11
12
/**
13
 * Class FileShowerAbstract
14
 *
15
 * Show details about the pipelines file on text output
16
 *
17
 * @package Ktomk\Pipelines\Utility\Show
18
 */
19
class FileShowerAbstract
20
{
21
    /**
22
     * @var callable
23
     */
24
    protected $output;
25
26
    /**
27
     * @var File
28
     */
29
    protected $file;
30
31
    /**
32
     * FileInfo constructor.
33
     *
34
     * @param callable $output
35
     * @param File $file
36
     */
37 16
    public function __construct($output, File $file)
38
    {
39 16
        $this->output = $output;
40 16
        $this->file = $file;
41 16
    }
42
43
    /**
44
     * @param string $message
45
     *
46
     * @return void
47
     */
48 15
    protected function info($message)
49
    {
50 15
        call_user_func($this->output, $message);
51 15
    }
52
53
    /* table show routines */
54
55
    /**
56
     * @param Pipelines $pipelines
57
     * @param array $table
58
     * @param int $errors
59
     *
60
     * @return array
61
     */
62 10
    protected function tablePipelineIdsPipelines(Pipelines $pipelines, array &$table, &$errors)
63
    {
64 10
        $return = array();
65
66 10
        foreach ($pipelines->getPipelineIds() as $id) {
67 10
            list($pipeline, $message) = $this->getShowPipeline($pipelines, $id);
68 10
            if ($message) {
69 5
                $table[] = array($id, 'ERROR', $message);
70 5
                $errors++;
71
72 5
                continue;
73
            }
74 5
            $return[$id] = $pipeline;
75
        }
76
77 10
        return $return;
78
    }
79
80
    /* text table implementation */
81
82
    /**
83
     * @param array $table
84
     *
85
     * @return void
86
     */
87 12
    protected function textTable(array $table)
88
    {
89 12
        $sizes = $this->textTableGetSizes($table);
90
91 12
        foreach ($table as $row) {
92 12
            $line = $this->textTableGetRow($row, $sizes);
93 12
            $this->info($line);
94
        }
95 12
    }
96
97
    /**
98
     * @param Pipelines $pipelines
99
     * @param string $id
100
     *
101
     * @return array
102
     */
103 10
    private function getShowPipeline(Pipelines $pipelines, $id)
104
    {
105 10
        $pipeline = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $pipeline is dead and can be removed.
Loading history...
106 10
        $message = null;
107
108
        try {
109 10
            $pipeline = $pipelines->getById($id);
110 5
        } catch (ParseException $exception) {
111 4
            $message = $exception->getParseMessage();
112 1
        } catch (InvalidArgumentException $exception) {
113 1
            $message = $exception->getMessage();
114
        }
115
116 10
        return array($pipeline, $message);
117
    }
118
119
    /**
120
     * get max sizes for each column in array table
121
     *
122
     * @param array $table
123
     *
124
     * @return array|int[] sizes
125
     */
126 12
    private function textTableGetSizes(array $table)
127
    {
128 12
        $sizes = array();
129 12
        foreach ($table[0] as $index => $cell) {
130 12
            $sizes[$index] = 0;
131
        }
132
133 12
        foreach ($table as $row) {
134 12
            foreach ($row as $index => $column) {
135 12
                $sizes[$index] = max($sizes[$index], strlen($column));
136
            }
137
        }
138
139 12
        return $sizes;
140
    }
141
142
    /**
143
     * @param array|string[] $row
144
     * @param array|int[] $sizes
145
     *
146
     * @return string
147
     */
148 12
    private function textTableGetRow(array $row, array $sizes)
149
    {
150 12
        $line = '';
151 12
        foreach ($row as $index => $column) {
152 12
            $len = strlen($column);
153 12
            $index && $line .= '    ';
154 12
            $line .= $column;
155 12
            $line .= str_repeat(' ', $sizes[$index] - $len);
156
        }
157
158 12
        return $line;
159
    }
160
}
161