Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

CreateCommand::execute()   B

Complexity

Conditions 9
Paths 24

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 7.4917
c 0
b 0
f 0
cc 9
nc 24
nop 2

How to fix   Long Method   

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
namespace N98\Magento\Command\Config\Env;
4
5
use Adbar\Dot;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use RecursiveArrayIterator;
8
use RecursiveIteratorIterator;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ConfirmationQuestion;
12
use Symfony\Component\Console\Question\Question;
13
14
/**
15
 * Class EnvCreateCommand
16
 *
17
 * This class is a port of EnvCreateCommand by Peter Jaap.
18
 * Thanks for allowing us to use the code in n98-magerun2.
19
 *
20
 * @see https://github.com/elgentos/magerun2-addons/blob/master/src/Elgentos/EnvCreateCommand.php
21
 * @package N98\Magento\Command\Config\Env
22
 */
23
class CreateCommand extends AbstractMagentoCommand
24
{
25
    /**
26
     * @var InputInterface
27
     */
28
    protected $input;
29
30
    /**
31
     * @var OutputInterface
32
     */
33
    protected $output;
34
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('config:env:create')
39
            ->setDescription('Create env file interactively')
40
            ->setHelp('Creates or modifies existing env.php file.');
41
    }
42
43
    /**
44
     * @param \Symfony\Component\Console\Input\InputInterface $input
45
     * @param \Symfony\Component\Console\Output\OutputInterface $output
46
     * @return int|void
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $questionHelper = $this->getHelperSet()->get('question');
51
52
        $this->detectMagento($output);
53
54
        $updateEnvQuestion = new ConfirmationQuestion(
55
            '<question>env file found. Do you want to update it?</question> <comment>[Y/n]</comment> ',
56
            true
57
        );
58
59
        $envFilePath = $this->getApplication()->getMagentoRootFolder() . '/app/etc/env.php';
60
61
        if (file_exists($envFilePath) && $questionHelper->ask($input, $output, $updateEnvQuestion)) {
62
            $envConfig = include $envFilePath;
63
        } else {
64
            $envConfig = include __DIR__ . '/stubs/env.php';
65
        }
66
67
        $env = new Dot($envConfig);
68
69
        $iterator = new RecursiveIteratorIterator(
70
            new RecursiveArrayIterator($env->all()),
71
            RecursiveIteratorIterator::SELF_FIRST
72
        );
73
74
        foreach ($iterator as $default) {
75
            if (!$iterator->hasChildren()) {
0 ignored issues
show
Bug introduced by
The method hasChildren() does not exist on RecursiveIteratorIterator. Did you maybe mean callHasChildren()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76
                for ($p = [], $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
77
                    $p[] = $iterator->getSubIterator($i)->key();
78
                }
79
80
                $path = implode('.', $p);
81
                $default = $this->getDefaultValue($path, $default);
82
                $question = new Question(
83
                    '<question>' . $path . '</question> <comment>[' . $default . ']</comment> ',
84
                    $default
85
                );
86
87
                $newValue = $questionHelper->ask($input, $output, $question);
88
                $env->set($path, $newValue);
89
            }
90
        }
91
92
        if (!file_exists('app/etc')) {
93
            if (!mkdir('app/etc', 0777, true) && !is_dir('app/etc')) {
94
                throw new \RuntimeException(sprintf('Directory "%s" was not created', 'app/etc'));
95
            }
96
        }
97
98
        file_put_contents($envFilePath, "<?php\n\nreturn " . EnvHelper::exportVariable($env->all()) . ";\n");
99
    }
100
101
    /**
102
     * @param string $path
103
     * @param $default
104
     * @return false|string
105
     */
106
    private function getDefaultValue(string $path, $default)
107
    {
108
        if ($path === 'install.date' && empty($default)) {
109
            return date('D, d M Y H:i:s T');
110
        }
111
112
        return $default;
113
    }
114
}
115