Completed
Push — master ( 699625...9c698c )
by grégoire
01:45
created

InspectDatabase::formatOutput()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 21

Duplication

Lines 9
Ratio 28.13 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 32
rs 8.8571
cc 2
eloc 21
nc 2
nop 5
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\Foundation\ResultIterator;
13
use Symfony\Component\Console\Helper\Table;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * InspectDatabase
19
 *
20
 * Return the list of schemas in the current database.
21
 *
22
 * @package   Cli
23
 * @copyright 2014 - 2015 Grégoire HUBERT
24
 * @author    Grégoire HUBERT
25
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
26
 * @see       PommAwareCommand
27
 */
28
class InspectDatabase extends SessionAwareCommand
29
{
30
    /**
31
     * configure
32
     *
33
     * @see Command
34
     */
35
    public function configure()
36
    {
37
        $this
38
            ->setName('pomm:inspect:database')
39
            ->setDescription('Show schemas in the current database.')
40
            ;
41
42
        parent::configure();
43
    }
44
45
    /**
46
     * execute
47
     *
48
     * @see Command
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        parent::execute($input, $output);
53
        $info = $this->getSession()->getInspector('schema')->getUserSchemas();
54
        $version = $this->getSession()->getInspector('database')->getVersion();
55
        $size = $this->getSession()->getInspector('database')->getSizePretty();
56
        $name = $this->getSession()->getInspector('database')->getName();
57
        $this->formatOutput($output, $info, $version, $size, $name);
58
    }
59
60
    /**
61
     * formatOutput
62
     *
63
     * Format command output from the inspector's result.
64
     *
65
     * @access protected
66
     * @param  OutputInterface  $output
67
     * @param  ResultIterator   $iterator
68
     * @return null
69
     */
70
    protected function formatOutput(OutputInterface $output, ResultIterator $iterator, $version, $size, $name)
71
    {
72
        $output->writeln(
73
            sprintf(
74
                "PostgreSQL v.%s",
75
                $version
76
            )
77
        );
78
        $output->writeln(
79
            sprintf(
80
                "Found <info>%d</info> schemas in database \"%s\" (%s).",
81
                $iterator->count(),
82
                $name,
83
                $size
84
            )
85
        );
86
        $table = (new Table($output))
87
            ->setHeaders(['name', 'oid ', 'relations', 'owner', 'comment'])
88
            ;
89
90 View Code Duplication
        foreach ($iterator as $schema_info) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
            $table->addRow([
92
                sprintf("<fg=yellow>%s</fg=yellow>", $schema_info['name']),
93
                $schema_info['oid'],
94
                $schema_info['relations'],
95
                $schema_info['owner'],
96
                wordwrap($schema_info['comment'])
97
            ]);
98
        }
99
100
        $table->render();
101
    }
102
}
103