CliRenderer   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
eloc 39
c 0
b 0
f 0
dl 0
loc 91
rs 10
ccs 46
cts 46
cp 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fillCells() 0 10 3
A __construct() 0 4 1
A renderData() 0 15 2
A render() 0 3 1
A withOrderDirection() 0 5 4
A withFilter() 0 3 2
A fillHeaders() 0 12 4
A getPager() 0 6 2
A getTableEngine() 0 3 1
1
<?php
2
3
namespace kalanis\kw_table\output_cli;
4
5
6
use kalanis\kw_clipr\Output\PrettyTable;
7
use kalanis\kw_table\core\Interfaces;
8
use kalanis\kw_table\core\Table;
9
use kalanis\kw_table\core\TableException;
10
11
12
/**
13
 * Class CliRenderer
14
 * @package kalanis\kw_table\output_cli
15
 * Render output to Cli
16
 */
17
class CliRenderer extends Table\AOutput
18
{
19
    protected const HEADER_PARAM_SEPARATOR = ':';
20
    protected PrettyTable $prettyTable;
21
22 9
    public function __construct(Table $table)
23
    {
24 9
        parent::__construct($table);
25 9
        $this->prettyTable = new PrettyTable();
26 9
    }
27
28
    /**
29
     * @return string
30
     */
31 9
    public function render(): string
32
    {
33 9
        return implode(PHP_EOL, $this->renderData());
34
    }
35
36
    /**
37
     * @return array<string>
38
     */
39 9
    public function renderData(): array
40
    {
41 9
        $this->fillHeaders();
42 9
        $this->fillCells();
43
44 9
        $lines = [];
45 9
        $lines[] = $this->prettyTable->getSeparator();
46 9
        $lines[] = $this->prettyTable->getHeader();
47 9
        $lines[] = $this->prettyTable->getSeparator();
48 9
        foreach ($this->prettyTable as $row) {
49 9
            $lines[] = strval($row);
50
        }
51 9
        $lines[] = $this->prettyTable->getSeparator();
52 9
        $lines[] = $this->getPager();
53 9
        return $lines;
54
    }
55
56 9
    protected function fillHeaders(): void
57
    {
58 9
        $order = $this->table->getOrderOrNull();
59 9
        $line = [];
60 9
        foreach ($this->table->getColumns() as $column) {
61 9
            if ($order && $order->isInOrder($column)) {
62 4
                $line[] = $this->withOrderDirection($order, $column) . $this->withFilter($column) . static::HEADER_PARAM_SEPARATOR . $column->getHeaderText();
63
            } else {
64 9
                $line[] = $this->withFilter($column) . static::HEADER_PARAM_SEPARATOR . $column->getHeaderText();
65
            }
66
        }
67 9
        $this->prettyTable->setHeaders($line);
68 9
    }
69
70 4
    protected function withOrderDirection(Table\Order $order, Interfaces\Table\IColumn $column): string
71
    {
72 4
        return Table\Order::ORDER_ASC == $order->getActiveDirection($column)
73 4
            ? ($order->isActive($column) ? '*^' : 'v')
74 4
            : ($order->isActive($column) ? '*v' : '^')
75
        ;
76
    }
77
78 9
    protected function withFilter(Interfaces\Table\IColumn $column): string
79
    {
80 9
        return ($column->hasHeaderFilterField() ? '>' : '');
81
    }
82
83 9
    protected function fillCells(): void
84
    {
85 9
        foreach ($this->table->getTableData() as $row) {
86
            /** @var Table\Internal\Row $row */
87 9
            $line = [];
88 9
            foreach ($row as $column) {
89
                /** @var Interfaces\Table\IColumn $column */
90 9
                $line[] = $column->getValue($row->getSource());
0 ignored issues
show
Bug introduced by
It seems like $row->getSource() can also be of type null; however, parameter $source of kalanis\kw_table\core\In...ble\IColumn::getValue() does only seem to accept kalanis\kw_connect\core\Interfaces\IRow, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
                $line[] = $column->getValue(/** @scrutinizer ignore-type */ $row->getSource());
Loading history...
91
            }
92 9
            $this->prettyTable->setDataLine($line);
93
        }
94 9
    }
95
96 9
    protected function getPager(): string
97
    {
98
        try {
99 9
            return PHP_EOL . $this->table->getPager()->render() . PHP_EOL;
100 5
        } catch (TableException $ex) {
101 5
            return '';
102
        }
103
    }
104
105 1
    public function getTableEngine(): PrettyTable
106
    {
107 1
        return $this->prettyTable;
108
    }
109
}
110