Completed
Push — master ( 565628...6dc795 )
by Greg
03:21
created

TableFormatter::configure()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Symfony\Component\Console\Helper\Table;
6
7
use Consolidation\OutputFormatters\FormatterInterface;
8
use Consolidation\OutputFormatters\ConfigureInterface;
9
use Consolidation\OutputFormatters\ValidationInterface;
10
use Consolidation\OutputFormatters\StructuredData\TableDataInterface;
11
use Consolidation\OutputFormatters\Transformations\PropertyParser;
12
use Consolidation\OutputFormatters\Transformations\ReorderFields;
13
use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
14
15
class TableFormatter implements FormatterInterface, ConfigureInterface, ValidationInterface
16
{
17
    protected $fieldLabels;
18
    protected $defaultFields;
19
    protected $tableStyle;
20
21 7
    public function __construct()
22
    {
23 7
        $this->tableStyle = 'default';
24 7
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29 7
    public function configure($configurationData)
30
    {
31 7
        if (isset($configurationData['table-style'])) {
32 1
            $this->tableStyle = $configurationData['table-style'];
33 1
        }
34 7
    }
35
36 7 View Code Duplication
    public function validate($structuredData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        // If the provided data was of class RowsOfFields
39
        // or AssociativeList, it will be converted into
40
        // a TableTransformation object by the restructure call.
41 7
        if (!$structuredData instanceof TableDataInterface) {
42 2
            throw new IncompatibleDataException(
43 2
                $this,
44 2
                $structuredData,
45
                [
46 2
                    new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'),
47 2
                    new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'),
48
                ]
49 2
            );
50
        }
51 5
        return $structuredData;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 5
    public function write(OutputInterface $output, $tableTransformer, $options = [])
58
    {
59
        $options += [
60 5
            'table-style' => $this->tableStyle,
61 5
            'include-field-labels' => true,
62
        ];
63
64 5
        $table = new Table($output);
65 5
        $table->setStyle($options['table-style']);
66 5
        $headers = $tableTransformer->getHeaders();
67 5
        $isList = $tableTransformer->isList();
68 5
        $includeHeaders = $options['include-field-labels'];
69 5
        if ($includeHeaders && !$isList && !empty($headers)) {
70 3
            $table->setHeaders($headers);
71 3
        }
72 5
        $table->setRows($tableTransformer->getTableData($includeHeaders && $isList));
73 5
        $table->render();
74 5
    }
75
}
76