Completed
Push — feature/add-cli-installers ( 1b9f9b...f84756 )
by Steven
13:07
created

Options::verifyAuth()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 3
eloc 11
nc 2
nop 3
1
<?php namespace Magestead\Helper;
2
3
use Symfony\Component\Console\Question\Question;
4
use Symfony\Component\Console\Input\InputInterface;
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Question\ChoiceQuestion;
7
use Symfony\Component\Console\Question\ConfirmationQuestion;
8
9
class Options
10
{
11
    protected $_app;
12
    protected $_server;
13
    protected $_phpVer = '56';
14
    protected $_box;
15
    protected $_m2Username;
16
    protected $_m2Password;
17
    protected $_ipAddress;
18
    protected $_cpus;
19
    protected $_memorylimit;
20
    protected $_repoUrl = '';
21
22
    /**
23
     * Options constructor.
24
     * @param $helper
25
     * @param InputInterface $input
26
     * @param OutputInterface $output
27
     */
28
    public function __construct($helper, InputInterface $input, OutputInterface $output)
29
    {
30
        $this->getVagrantSettings($helper, $input, $output);
31
        $this->getApplicationSettings($helper, $input, $output);
32
        $this->getMagento2Settings($helper, $input, $output);
33
        $this->getVersionControlSettings($helper, $input, $output);
34
35
        $this->_box = "centos65+$this->_server+php$this->_phpVer";
36
    }
37
    /**
38
     * @return array
39
     */
40
    public function getAllOptions()
41
    {
42
        return [
43
          'app' => $this->getApp(),
44
          'server' => $this->getServer(),
45
          'phpver' => $this->getPhpVer(),
46
          'box' => $this->getBox(),
47
          'm2user' => $this->getM2Username(),
48
          'm2pass' => $this->getM2Password(),
49
          'repo_url' => $this->getRepoUrl(),
50
          'ip_address' => $this->getIpAddress(),
51
          'cpus' => $this->getCpus(),
52
          'memory_limit' => $this->getMemorylimit(),
53
        ];
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    public function getApp()
60
    {
61
        return $this->_app;
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67
    public function getServer()
68
    {
69
        return $this->_server;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getPhpVer()
76
    {
77
        return $this->_phpVer;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getBox()
84
    {
85
        return $this->_box;
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91
    public function getM2Username()
92
    {
93
        return $this->_m2Username;
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99
    public function getM2Password()
100
    {
101
        return $this->_m2Password;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getMemorylimit()
108
    {
109
        return $this->_memorylimit;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getIpAddress()
116
    {
117
        return $this->_ipAddress;
118
    }
119
120
    /**
121
     * @return mixed
122
     */
123
    public function getCpus()
124
    {
125
        return $this->_cpus;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getRepoUrl()
132
    {
133
        return $this->_repoUrl;
134
    }
135
136
    /**
137
     * @param $helper
138
     * @param InputInterface $input
139
     * @param OutputInterface $output
140
     */
141
    protected function getVagrantSettings($helper, InputInterface $input, OutputInterface $output)
142
    {
143
        $output->writeln('<comment>Lets configure your project\'s VM</comment>');
144
145
        $ipQuestion = new Question("Configure the IP for your VM (192.168.47.47): ", '192.168.47.47');
146
        $this->_ipAddress = strtolower($helper->ask($input, $output, $ipQuestion));
147
148
        $cpuQuestion = new Question("How many CPU's should we use? (1): ", '1');
149
        $this->_cpus = strtolower($helper->ask($input, $output, $cpuQuestion));
150
151
        $memoryQuestion = new Question("Enter VM memory limit (2048): ", '2048');
152
        $this->_memorylimit = strtolower($helper->ask($input, $output, $memoryQuestion));
153
    }
154
155
    /**
156
     * @param $helper
157
     * @param InputInterface $input
158
     * @param OutputInterface $output
159
     */
160
    protected function getApplicationSettings($helper, InputInterface $input, OutputInterface $output)
161
    {
162
        $output->writeln('<comment>Lets configure your project\'s application</comment>');
163
        $appQuestion = new ChoiceQuestion(
164
            "Which application do you want to install?",
165
            ['Magento', 'Magento 2'],
166
            0
167
        );
168
        $this->_app = strtolower($helper->ask($input, $output, $appQuestion));
169
170
        $serverQuestion = new ChoiceQuestion(
171
            "Which webserver would you like?",
172
            ['NGINX', 'Apache'],
173
            0
174
        );
175
        $this->_server = strtolower($helper->ask($input, $output, $serverQuestion));
176
    }
177
178
    /**
179
     * @param $helper
180
     * @param InputInterface $input
181
     * @param OutputInterface $output
182
     * @return bool
183
     */
184
    protected function getMagento2Settings($helper, InputInterface $input, OutputInterface $output)
185
    {
186
        // todo add php 7 enabled box
187
        // $this->usePhp7($helper, $input, $output);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
189
        if ($this->_app === 'magento 2') {
190
            return $this->verifyAuth($helper, $input, $output);
191
        }
192
193
        return true;
194
    }
195
196
    /**
197
     * @param $helper
198
     * @param InputInterface $input
199
     * @param OutputInterface $output
200
     */
201
    protected function getVersionControlSettings($helper, InputInterface $input, OutputInterface $output)
202
    {
203
        $versionControl = new ConfirmationQuestion("Would you like to add your project to GIT? (yes/no) ", true);
204
        $versioning = $helper->ask($input, $output, $versionControl);
205
        if ($versioning) {
206
            $repoQuestion = new Question("Enter your full GitHub/BitBucket repo URL: ", '');
207
            $this->_repoUrl = strtolower($helper->ask($input, $output, $repoQuestion));
208
        }
209
    }
210
211
    /**
212
     * @param $helper
213
     * @param InputInterface $input
214
     * @param OutputInterface $output
215
     */
216
    protected function askForAuth($helper, InputInterface $input, OutputInterface $output)
217
    {
218
        $username = new Question("Please enter your Magento username (public key): ", '');
219
        $this->_m2Username = $helper->ask($input, $output, $username);
220
221
        $password = new Question("Please enter your Magento password (private key): ", '');
222
        $this->_m2Password = $helper->ask($input, $output, $password);
223
    }
224
225
    /**
226
     * @param $helper
227
     * @param InputInterface $input
228
     * @param OutputInterface $output
229
     * @return bool
230
     */
231
    protected function verifyAuth($helper, InputInterface $input, OutputInterface $output)
0 ignored issues
show
Coding Style introduced by
verifyAuth uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
232
    {
233
        $authFile = $_SERVER['HOME'] . "/.composer/auth.json";
234
235
        $authJson = file_get_contents($authFile);
236
        $authObj = (array)json_decode($authJson);
237
238
        if (isset($authObj['http-basic']) && isset($authObj['http-basic']->{'repo.magento.com'})) {
239
            return true;
240
        }
241
242
        $this->askForAuth($helper, $input, $output);
243
244
        $authObj['http-basic']['repo.magento.com']['username'] = $this->_m2Username;
245
        $authObj['http-basic']['repo.magento.com']['password'] = $this->_m2Password;
246
247
        $authJson = json_encode($authObj);
248
        return file_put_contents($authFile, $authJson);
249
    }
250
251
    /**
252
     * @param $helper
253
     * @param InputInterface $input
254
     * @param OutputInterface $output
255
     */
256
    protected function usePhp7($helper, InputInterface $input, OutputInterface $output)
257
    {
258
        if ($this->_app !== 'magento' && $this->_server !== 'apache') {
259
            $phpVerQuestion = new ChoiceQuestion(
260
                "Which version of PHP should be installed?",
261
                ['56', '7'],
262
                0
263
            );
264
            $this->_phpVer = $helper->ask($input, $output, $phpVerQuestion);
265
        }
266
    }
267
}