Completed
Push — develop ( c8bccb...27dcf1 )
by Tom
04:44
created

DownloadMagento   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 15
c 9
b 2
f 0
lcom 1
cbo 9
dl 0
loc 160
rs 10

4 Methods

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