TableRenderer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 76
wmc 12
lcom 1
cbo 3
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 2
A canDrawTables() 0 3 2
A getRows() 0 3 1
A getHeaders() 0 3 1
A render() 0 16 2
A prepareData() 0 13 3
A __construct() 0 3 1
1
<?php
2
namespace rtens\domin\delivery\cli\renderers\tables;
3
4
use League\CLImate\CLImate;
5
use rtens\domin\delivery\Renderer;
6
use rtens\domin\delivery\RendererRegistry;
7
use rtens\domin\delivery\web\renderers\tables\Table;
8
9
class TableRenderer implements Renderer {
10
11
    /** @var RendererRegistry */
12
    private $renderers;
13
14
    public function __construct(RendererRegistry $renderers) {
15
        $this->renderers = $renderers;
16
    }
17
18
    /**
19
     * @param mixed $value
20
     * @return bool
21
     */
22
    public function handles($value) {
23
        return $value instanceof Table && $this->canDrawTables();
24
    }
25
26
    /**
27
     * @param Table $value
28
     * @return mixed
29
     */
30
    public function render($value) {
31
        $climate = new CLImate();
32
        $climate->output->defaultTo('buffer');
33
34
        $data = $this->prepareData($value);
35
        if (!$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36
            return '(empty)';
37
        }
38
39
        $climate->table($data);
40
41
        /** @var \League\CLImate\Util\Writer\Buffer $buffer */
42
        $buffer = $climate->output->get('buffer');
43
44
        return PHP_EOL . $buffer->get();
45
    }
46
47
    /**
48
     * @param Table $object
49
     * @return array
50
     */
51
    protected function prepareData($object) {
52
        $headers = $this->getHeaders($object);
53
54
        $data = [];
55
        foreach ($this->getRows($object) as $row) {
56
            $dataRow = [];
57
            foreach ($row as $col => $value) {
58
                $dataRow[$headers[$col]] = str_replace("\n", " ", $this->renderers->getRenderer($value)->render($value));
59
            }
60
            $data[] = $dataRow;
61
        }
62
        return $data;
63
    }
64
65
    protected function canDrawTables() {
66
        return (extension_loaded('mbstring') && class_exists(CLImate::class));
67
    }
68
69
    /**
70
     * @param Table $table
71
     * @return mixed
72
     */
73
    protected function getRows($table) {
74
        return $table->getItems();
75
    }
76
77
    /**
78
     * @param Table $table
79
     * @return mixed
80
     */
81
    protected function getHeaders($table) {
82
        return $table->getHeaders();
83
    }
84
}