InspectRelation::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/*
3
 * This file is part of Pomm's Cli package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Cli\Command;
11
12
use PommProject\Cli\Exception\CliException;
13
use PommProject\Foundation\ConvertedResultIterator;
14
use PommProject\Foundation\Exception\SqlException;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Helper\Table;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * InspectRelation
22
 *
23
 * Display information about a given relation.
24
 *
25
 * @package   Cli
26
 * @copyright 2014 - 2015 Grégoire HUBERT
27
 * @author    Grégoire HUBERT
28
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
29
 * @see       SchemaAwareCommand
30
 */
31
class InspectRelation extends RelationAwareCommand
32
{
33
    /**
34
     * configure
35
     *
36
     * @see Command
37
     */
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('pomm:inspect:relation')
42
            ->setDescription('Display a relation information.')
43
        ;
44
        parent::configure();
45
    }
46
47
    /**
48
     * execute
49
     *
50
     * @see Command
51
     * @throws CliException
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        parent::execute($input, $output);
56
        $this->relation = $input->getArgument('relation');
57
        $inspector = $this->getSession()
58
            ->getInspector('relation')
59
            ;
60
        try {
61
            $relation_size = $inspector
62
                ->getTableTotalSizeOnDisk($this->schema, $this->relation);
63
        } catch (SqlException $e) {
64
            throw new CliException(
65
                sprintf(
66
                    "Relation '%s.%s' not found.\nRelations in this schema are {%s}.",
67
                    $this->schema,
68
                    $this->relation,
69
                    join(', ', $inspector->getRelationsInSchema($this->schema)->slice('name'))
70
                )
71
            );
72
        }
73
74
        $fields_infos = $inspector->getTableFieldInformationName($this->schema, $this->relation);
75
76
        $this->formatOutput($output, $fields_infos, $relation_size);
77
78
        return 0;
79
    }
80
81
    /**
82
     * formatOutput
83
     *
84
     * Render output.
85
     *
86
     * @access protected
87
     * @param  OutputInterface         $output
88
     * @param  ConvertedResultIterator $fields_infos
89
     * @param  int                     $size
90
     * @return void
91
     */
92
    protected function formatOutput(OutputInterface $output, ConvertedResultIterator $fields_infos, $size)
93
    {
94
        $output->writeln(
95
            sprintf(
96
                "Relation <fg=cyan>%s.%s</fg=cyan> (size with indexes: %d bytes)",
97
                $this->schema,
98
                $this->relation,
99
                $size
100
            )
101
        );
102
        $table = (new Table($output))
103
            ->setHeaders(['pk', 'name', 'type', 'default', 'notnull', 'comment'])
104
            ;
105
106
        foreach ($fields_infos as $info) {
107
            $table->addRow(
108
                [
109
                    $info['is_primary'] ? '<fg=cyan>*</fg=cyan>' : '',
110
                    sprintf("<fg=yellow>%s</fg=yellow>", $info['name']),
111
                    $this->formatType($info['type']),
112
                    $info['default'],
113
                    $info['is_notnull'] ? 'yes' : 'no',
114
                    wordwrap($info['comment']),
115
                ]
116
            );
117
        }
118
119
        $table->render();
120
    }
121
122
    /**
123
     * formatType
124
     *
125
     * Format type.
126
     *
127
     * @access protected
128
     * @param  string $type
129
     * @return string
130
     */
131
    protected function formatType($type)
132
    {
133
        if (preg_match('/^(?:(.*)\.)?_(.*)$/', $type, $matches)) {
134
            if ($matches[1] !== '') {
135
                return sprintf("%s.%s[]", $matches[1], $matches[2]);
136
            } else {
137
                return $matches[2].'[]';
138
            }
139
        } else {
140
            return $type;
141
        }
142
    }
143
}
144