Completed
Push — develop ( f8e6eb...81325c )
by Tom
04:33
created

DownloadMagento::execute()   B

Complexity

Conditions 8
Paths 107

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 56
rs 7.0067
cc 8
eloc 31
nc 107
nop 0

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\Installer\SubCommand;
4
5
use N98\Magento\Command\SubCommand\AbstractSubCommand;
6
use N98\Util\Console\Helper\ComposerHelper;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\ProcessBuilder;
9
10
class DownloadMagento extends AbstractSubCommand
11
{
12
    /**
13
     * @return void
14
     */
15
    public function execute()
16
    {
17
        if ($this->input->getOption('noDownload')) {
18
            return;
19
        }
20
21
        try {
22
            $this->checkMagentoConnectCredentials($this->output);
23
24
            $package = $this->config['magentoVersionData'];
25
            $this->config->setArray('magentoPackage', $package);
26
27
            if (file_exists($this->config->getString('installationFolder') . '/app/etc/env.php')) {
28
                $this->output->writeln('<error>A magento installation already exists in this folder </error>');
29
                return;
30
            }
31
32
            $args = [
33
                $this->config['composer_bin'],
34
                'create-project',
35
            ];
36
37
            // Add composer options
38
            foreach ($package['options'] as $optionName => $optionValue) {
39
                $args[] = '--' . $optionName . ($optionValue === true ? '' : '=' . $optionValue);
40
            }
41
42
            // Add arguments
43
            $args[] = $package['package'];
44
            $args[] = $this->config->getString('installationFolder');
45
            $args[] = $package['version'];
46
47
            if (OutputInterface::VERBOSITY_VERBOSE <= $this->output->getVerbosity()) {
48
                $args[] = '-vvv';
49
            }
50
51
            /**
52
             * @TODO use composer helper
53
             */
54
            $processBuilder = new ProcessBuilder($args);
55
56
            $process = $processBuilder->getProcess();
57
            $process->setInput($this->input);
58
            if (OutputInterface::VERBOSITY_VERBOSE <= $this->output->getVerbosity()) {
59
                $this->output->writeln($process->getCommandLine());
60
            }
61
62
            $process->setTimeout(86400);
63
            $process->start();
64
            $process->wait(function ($type, $buffer) {
65
                $this->output->write($buffer, false, OutputInterface::OUTPUT_RAW);
66
            });
67
        } catch (\Exception $e) {
68
            $this->output->writeln('<error>' . $e->getMessage() . '</error>');
69
        }
70
    }
71
72
    /**
73
     * construct a folder to where magerun will download the source to,
74
     * cache git/hg repositories under COMPOSER_HOME
75
     *
76
     * @param $composer
77
     * @param $package
78
     * @param $installationFolder
79
     *
80
     * @return string
81
     */
82
    protected function getTargetFolderByType($composer, $package, $installationFolder)
83
    {
84
        $type = $package->getSourceType();
85
        if ($this->getCommand()->isSourceTypeRepository($type)) {
86
            $targetPath = sprintf(
87
                '%s/%s/%s/%s',
88
                $composer->getConfig()->get('cache-dir'),
89
                '_n98_magerun_download',
90
                $type,
91
                preg_replace('{[^a-z0-9.]}i', '-', $package->getSourceUrl())
92
            );
93
        } else {
94
            $targetPath = sprintf(
95
                '%s/%s',
96
                $installationFolder,
97
                '_n98_magerun_download'
98
            );
99
        }
100
101
        return $targetPath;
102
    }
103
104
    /**
105
     * @param OutputInterface $output
106
     */
107
    protected function checkMagentoConnectCredentials(OutputInterface $output)
108
    {
109
        $configKey = 'http-basic.repo.magento.com';
110
111
        $composerHelper = $this->getCommand()->getHelper('composer');
112
        /** @var $composerHelper ComposerHelper */
113
        $authConfig = $composerHelper->getConfigValue($configKey);
114
115
        if (!isset($authConfig->username)
116
            || !isset($authConfig->password)
117
        ) {
118
            $this->output->writeln(array(
119
                '',
120
                $this->getCommand()
121
                    ->getHelperSet()
122
                    ->get('formatter')
123
                    ->formatBlock('Authentication', 'bg=blue;fg=white', true),
124
                '',
125
            ));
126
127
            $this->output->writeln(array(
128
                'You need to create a secury key. Login at magentocommerce.com.',
129
                'Developers -> Secure Keys. <info>Use public key as username and private key as password</info>',
130
                ''
131
            ));
132
            $dialog = $this->getCommand()->getHelper('dialog');
133
134
            $username = $dialog->askAndValidate(
135
                $output,
136
                '<comment>Please enter your public key: </comment>',
137
                function ($value) {
138
                    if ('' === trim($value)) {
139
                        throw new \Exception('The private key (auth token) can not be empty');
140
                    }
141
142
                    return $value;
143
                },
144
                20,
145
                false
146
            );
147
148
149
            $password = $dialog->askHiddenResponseAndValidate(
150
                $output,
151
                '<comment>Please enter your private key: </comment>',
152
                function ($value) {
153
                    if ('' === trim($value)) {
154
                        throw new \Exception('The private key (auth token) can not be empty');
155
                    }
156
157
                    return $value;
158
                },
159
                20,
160
                false
161
            );
162
163
            $composerHelper->setConfigValue($configKey, [$username, $password]);
164
        }
165
    }
166
}
167