Completed
Push — master ( 2dbc44...449b8b )
by Robert
03:25 queued 01:17
created

BaseOutput   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 96.08%

Importance

Changes 9
Bugs 2 Features 1
Metric Value
wmc 17
c 9
b 2
f 1
lcom 0
cbo 3
dl 0
loc 126
ccs 49
cts 51
cp 0.9608
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A printTable() 0 20 4
A printBoolean() 0 14 3
B printList() 0 15 5
A printProducts() 0 8 1
A printMessage() 0 4 1
A getColumnWidth() 0 9 3
1
<?php
2
3
namespace Rs\VersionEye\Output;
4
5
use Rs\VersionEye\Http\Pager;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Helper\TableHelper;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * BaseOutput.
12
 *
13
 * @author Robert Schönthal <[email protected]>
14
 */
15
abstract class BaseOutput
16
{
17
    /**
18
     * prints a table, values can be modified via $callback.
19
     *
20
     * @param OutputInterface $output
21
     * @param string[]        $headings
22
     * @param string[]        $keys
23
     * @param array|Pager     $data
24
     * @param \Closure        $callback
25
     */
26 13
    protected function printTable(OutputInterface $output, array $headings, array $keys, $data, \Closure $callback = null)
27
    {
28 13
        if (!class_exists('Symfony\Component\Console\Helper\Table')) {
29
            $table = new TableHelper(false);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Console\Helper\TableHelper has been deprecated with message: since version 2.5, to be removed in 3.0 Use {@link Table} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30
        } else {
31 13
            $table = new Table($output);
32
        }
33
34 13
        $table->setHeaders($headings);
35
36 13
        foreach ($data as $row) {
37 13
            $rowData = array_merge(array_flip($keys), array_intersect_key($row, array_flip($keys)));
38 13
            if ($callback) {
39 5
                $rowData = array_map($callback, array_keys($rowData), $rowData);
40 5
            }
41 13
            $table->addRow($rowData);
42 13
        }
43
44 13
        $table->render($output);
0 ignored issues
show
Unused Code introduced by
The call to Table::render() has too many arguments starting with $output.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45 13
    }
46
47
    /**
48
     * prints a simple boolean.
49
     *
50
     * @param OutputInterface $output
51
     * @param string          $success
52
     * @param string          $fail
53
     * @param bool            $value
54
     * @param bool            $line
55
     *
56
     * @return string
57
     */
58 18
    protected function printBoolean(OutputInterface $output, $success, $fail, $value, $line = true)
59
    {
60 18
        if ($value) {
61 14
            $message = '<info>' . $success . '</info>';
62 14
        } else {
63 8
            $message = '<error>' . $fail . '</error>';
64
        }
65
66 18
        if (false === $line) {
67 4
            return $message;
68
        }
69
70 14
        $output->writeln($message);
71 14
    }
72
73
    /**
74
     * prints a list combined as <comment>Heading</comment> : <info>Value</info>, values can be modified via $callback.
75
     *
76
     * @param OutputInterface $output
77
     * @param string[]        $headings
78
     * @param string[]        $keys
79
     * @param array           $data
80
     * @param \Closure        $callback
81
     */
82 10
    protected function printList(OutputInterface $output, array $headings, array $keys, array $data, \Closure $callback = null)
83
    {
84 10
        $width = $this->getColumnWidth($headings);
85 10
        $data  = array_merge(array_flip($keys), array_intersect_key($data, array_flip($keys)));
86
87 10
        foreach ($headings as $key => $heading) {
88 10
            $value = array_values($data)[$key];
89 10
            if ($callback) {
90 5
                $value = $callback($heading, $value);
91 5
            }
92 10
            $value = is_bool($value) ? (true === $value ? 'Yes' : 'No') : $value;
93
94 10
            $output->writeln(sprintf('<comment>%s%s</comment> : <info>%s</info>', $heading, str_repeat(' ', $width - strlen($heading)), $value));
95 10
        }
96 10
    }
97
98
    /**
99
     * output for references/search api.
100
     *
101
     * @param OutputInterface $output
102
     * @param array|Pager     $products
103
     */
104 6
    protected function printProducts(OutputInterface $output, $products)
105
    {
106 6
        $this->printTable($output,
107 6
            ['Name', 'Language', 'Version', 'Type'],
108 6
            ['name', 'language', 'version', 'prod_type'],
109
            $products
110 6
        );
111 6
    }
112
113
    /**
114
     * prints a simple message.
115
     *
116
     * @param OutputInterface $output
117
     * @param array           $response
118
     */
119 3
    protected function printMessage(OutputInterface $output, array $response)
120
    {
121 3
        $this->printBoolean($output, $response['message'], $response['message'], true === $response['success']);
122 3
    }
123
124
    /**
125
     * calculates the max width of a given set of string.
126
     *
127
     * @param string[] $headings
128
     *
129
     * @return int
130
     */
131 10
    private function getColumnWidth(array $headings)
132
    {
133 10
        $width = 0;
134 10
        foreach ($headings as $heading) {
135 10
            $width = strlen($heading) > $width ? strlen($heading) : $width;
136 10
        }
137
138 10
        return $width + 5;
139
    }
140
}
141