Completed
Push — master ( 5ea837...9dec3c )
by Paul
9s
created

ConfigDebugCommand::execute()   C

Complexity

Conditions 11
Paths 88

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 57
rs 6.4824
cc 11
eloc 35
nc 88
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Console\Command;
12
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Yaml\Dumper;
17
18
/**
19
 * Outputs all the configuration processed by the Framework, after merging.
20
 *
21
 * @author      Vítor Brandão <[email protected]>
22
 */
23
class ConfigDebugCommand extends AbstractCommand
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('config:debug')
32
            ->setDescription('Dumps the configuration in use')
33
            ->addOption('app-only', null, InputOption::VALUE_NONE, 'Show only the configuration set in the app/ directory')
34
            ->addOption('write-php', null, InputOption::VALUE_REQUIRED, 'Save the configuration in PHP format')
35
            ->addOption('write-yaml', null, InputOption::VALUE_REQUIRED, 'Save the configuration in YAML format')
36
            ->setHelp(<<<EOF
37
The <info>%command.name%</info> command dumps the configuration after being merged and processed
38
by the framework:
39
40
  <info>%command.full_name%</info>
41
42
If you only want to see the configuration defined in the app/ directory (excluding modules)
43
use the <info>--app-only</info> option. This is the "raw" configuration, not processed by the framework.
44
45
  <info>%command.full_name% --app-only</info>
46
EOF
47
            );
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $indentation = 4;
56
57
        if ($input->getOption('app-only')) {
58
            $message = "This is the configuration defined in the app/ directory (not processed):\n";
59
            $config  = $this->getServiceManager()->get('ApplicationConfig');
60
        } else {
61
            $message = "This is the configuration in use for your current setup (merged and processed):\n";
62
            $config  = $this->getServiceManager()->get('Config');
63
        }
64
65
        $files    = array();
66
        $contents = array();
67
68
        if (($files['php'] = $input->getOption('write-php'))) {
69
            $contents['php'] = "<?php\n\nreturn " . var_export($config, true) . ";\n\n?>\n";
70
        }
71
72
        if (($files['yaml'] = $input->getOption('write-yaml'))) {
73
            $dumper = new Dumper();
74
            $dumper->setIndentation($indentation);
75
            $contents['yaml'] = $dumper->dump($config, 6, 0, false, false);
76
        }
77
78
        if (empty($contents)) {
79
            $dumper = new Dumper();
80
            $dumper->setIndentation($indentation);
81
            $output->writeln($message);
82
83
            foreach ($config as $rootKey => $subConfig) {
84
                $output->writeln('<info>' . $rootKey . '</info>:');
85
                $output->writeln($dumper->dump($subConfig, 6, $indentation, false, false));
86
            }
87
88
            return;
89
        }
90
91
        foreach ($files as $format => $file) {
92
            $output->write('Saving configuration in <info>' . strtoupper($format) . '</info> format...');
93
            if ($fileExists = file_exists($file)) {
94
                if (!isset($dialog)) {
95
                    $dialog = $this->getHelperSet()->get('dialog');
96
                }
97
                if (!$dialog->askConfirmation($output,
98
                    " <question>File \"" . $file . "\" already exists. Proceed anyway?</question> ", false)) {
99
                    continue;
100
                }
101
            }
102
103
            file_put_contents($file, $contents[$format]);
104
105
            if (!$fileExists) {
106
                $output->writeln(' OK.');
107
            }
108
        }
109
    }
110
}
111