Completed
Pull Request — master (#177)
by
unknown
04:33
created

InfoCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 44
Code Lines 30

Duplication

Lines 5
Ratio 11.36 %
Metric Value
dl 5
loc 44
rs 8.439
cc 5
eloc 30
nc 12
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Database;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
10
11
class InfoCommand extends AbstractDatabaseCommand
12
{
13 View Code Duplication
    protected function configure()
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...
14
    {
15
        $this
16
            ->setName('db:info')
17
            ->addArgument('setting', InputArgument::OPTIONAL, 'Only output value of named setting')
18
            ->setDescription('Dumps database informations')
19
            ->addOption(
20
                'format',
21
                null,
22
                InputOption::VALUE_OPTIONAL,
23
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
24
            )
25
        ;
26
        $this->addDeprecatedAlias('database:info', 'Please use db:info');
27
28
        $help = <<<HELP
29
This command is useful to print all informations about the current configured database in app/etc/local.xml.
30
It can print connection string for JDBC, PDO connections.
31
HELP;
32
        $this->setHelp($help);
33
34
    }
35
36
    /**
37
     * @param \Symfony\Component\Console\Input\InputInterface   $input
38
     * @param \Symfony\Component\Console\Output\OutputInterface $output
39
     * @throws \InvalidArgumentException
40
     * @return void
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $this->detectDbSettings($output);
45
46
        $settings = array();
47
        foreach ($this->dbSettings as $key => $value) {
48
            $settings[$key] = (string) $value;
49
        }
50
51
        $pdoConnectionString = sprintf(
52
            'mysql:host=%s;dbname=%s',
53
            $this->dbSettings['host'],
54
            $this->dbSettings['dbname']
55
        );
56
        $settings['PDO-Connection-String'] = $pdoConnectionString;
57
58
        $jdbcConnectionString = sprintf(
59
            'jdbc:mysql://%s/%s?username=%s&password=%s',
60
            $this->dbSettings['host'],
61
            $this->dbSettings['dbname'],
62
            $this->dbSettings['username'],
63
            $this->dbSettings['password']
64
        );
65
        $settings['JDBC-Connection-String'] = $jdbcConnectionString;
66
67
        $mysqlCliString = 'mysql ' . $this->getHelper('database')->getMysqlClientToolConnectionString();
68
        $settings['MySQL-Cli-String'] = $mysqlCliString;
69
70
        $rows = array();
71
        foreach ($settings as $settingName => $settingValue) {
72
            $rows[] = array($settingName, $settingValue);
73
        }
74
75
        if (($settingArgument = $input->getArgument('setting')) !== null) {
76
            if (!isset($settings[$settingArgument])) {
77
                throw new \InvalidArgumentException('Unknown setting: ' . $settingArgument);
78
            }
79
            $output->writeln((string) $settings[$settingArgument]);
80 View Code Duplication
        } else {
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...
81
            $this->getHelper('table')
82
                ->setHeaders(array('Name', 'Value'))
83
                ->renderByFormat($output, $rows, $input->getOption('format'));
84
        }
85
    }
86
87
}
88