Completed
Push — epic/2.0.0 ( 250416...77a749 )
by Steven
14:57
created

Options::setVagrantBox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
/**
10
 * Class Options
11
 * @package Magestead\Helper
12
 */
13
class Options
14
{
15
    const BOX_PREFIX = 'richdynamix/magestead-';
16
    protected $_app;
17
    protected $_server;
18
    protected $_phpVer = '56';
19
    protected $_os = 'centos65';
20
    protected $_box;
21
    protected $_m2Username;
22
    protected $_m2Password;
23
    protected $_ipAddress;
24
    protected $_cpus;
25
    protected $_locale;
26
    protected $_currency;
27
    protected $_baseUrl;
28
    protected $_repoUrl = '';
29
30
    /**
31
     * Options constructor.
32
     * @param $helper
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     */
36
    public function __construct($helper, InputInterface $input, OutputInterface $output)
37
    {
38
        $this->getVagrantSettings($helper, $input, $output);
39
        $this->getApplicationSettings($helper, $input, $output);
40
        $this->getMagento2Settings($helper, $input, $output);
41
        $this->getVersionControlSettings($helper, $input, $output);
42
43
        $this->setVagrantBox();
44
45
    }
46
    /**
47
     * @return array
48
     */
49
    public function getAllOptions()
50
    {
51
        return [
52
          'app' => $this->getApp(),
53
          'server' => $this->getServer(),
54
          'phpver' => $this->getPhpVer(),
55
          'os' => $this->getOs(),
56
          'box' => $this->getBox(),
57
          'm2user' => $this->getM2Username(),
58
          'm2pass' => $this->getM2Password(),
59
          'repo_url' => $this->getRepoUrl(),
60
          'ip_address' => $this->getIpAddress(),
61
          'cpus' => $this->getCpus(),
62
          'memory_limit' => $this->getMemorylimit(),
63
          'locale' => $this->getLocale(),
64
          'default_currency' => $this->getCurrency(),
65
          'base_url' => $this->getBaseUrl(),
66
        ];
67
    }
68
69
    /**
70
     * @return mixed
71
     */
72
    public function getApp()
73
    {
74
        return $this->_app;
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    public function getServer()
81
    {
82
        return $this->_server;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getPhpVer()
89
    {
90
        return $this->_phpVer;
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public function getBox()
97
    {
98
        return $this->_box;
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    public function getM2Username()
105
    {
106
        return $this->_m2Username;
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function getM2Password()
113
    {
114
        return $this->_m2Password;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getMemorylimit()
121
    {
122
        return $this->_memorylimit;
0 ignored issues
show
Bug introduced by
The property _memorylimit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getIpAddress()
129
    {
130
        return $this->_ipAddress;
131
    }
132
133
    /**
134
     * @return mixed
135
     */
136
    public function getCpus()
137
    {
138
        return $this->_cpus;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getRepoUrl()
145
    {
146
        return $this->_repoUrl;
147
    }
148
149
    /**
150
     * @return mixed
151
     */
152
    public function getLocale()
153
    {
154
        return $this->_locale;
155
    }
156
157
    /**
158
     * @return mixed
159
     */
160
    public function getCurrency()
161
    {
162
        return $this->_currency;
163
    }
164
165
    /**
166
     * @return mixed
167
     */
168
    public function getBaseUrl()
169
    {
170
        return $this->_baseUrl;
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function getOs()
177
    {
178
        return $this->_os;
179
    }
180
181
    /**
182
     * @param $helper
183
     * @param InputInterface $input
184
     * @param OutputInterface $output
185
     */
186
    protected function getVagrantSettings($helper, InputInterface $input, OutputInterface $output)
187
    {
188
        $output->writeln('<comment>Lets configure your project\'s VM</comment>');
189
190
        $ipQuestion = new Question("Configure the IP for your VM (192.168.47.47): ", '192.168.47.47');
191
        $this->_ipAddress = strtolower($helper->ask($input, $output, $ipQuestion));
192
193
        $cpuQuestion = new Question("How many CPU's would you like to use? (1): ", '1');
194
        $this->_cpus = strtolower($helper->ask($input, $output, $cpuQuestion));
195
196
        $memoryQuestion = new Question("Define the VM memory limit (2048): ", '2048');
197
        $this->_memorylimit = strtolower($helper->ask($input, $output, $memoryQuestion));
198
    }
199
200
    /**
201
     * @param $helper
202
     * @param InputInterface $input
203
     * @param OutputInterface $output
204
     */
205
    protected function getApplicationSettings($helper, InputInterface $input, OutputInterface $output)
206
    {
207
        $output->writeln('<comment>Lets configure your project\'s application</comment>');
208
        $appQuestion = new ChoiceQuestion(
209
            "Which application do you want to install?",
210
            ['Magento', 'Magento 2'],
211
            0
212
        );
213
        $this->_app = strtolower($helper->ask($input, $output, $appQuestion));
214
215
        $baseUrlQuestion = new Question("Enter your application's base_url (magestead.dev): ", 'magestead.dev');
216
        $this->_baseUrl = strtolower($helper->ask($input, $output, $baseUrlQuestion));
217
218
        $currenyQuestion = new Question("Enter your application's default currency (GBP): ", 'GBP');
219
        $this->_currency = $helper->ask($input, $output, $currenyQuestion);
220
221
        $localeQuestion = new Question("Enter your application's default locale (en_GB): ", 'en_GB');
222
        $this->_locale = $helper->ask($input, $output, $localeQuestion);
223
224
        $serverQuestion = new ChoiceQuestion(
225
            "Which webserver would you like?",
226
            ['NGINX', 'Apache'],
227
            0
228
        );
229
        $this->_server = strtolower($helper->ask($input, $output, $serverQuestion));
230
    }
231
232
    /**
233
     * @param $helper
234
     * @param InputInterface $input
235
     * @param OutputInterface $output
236
     * @return bool
237
     */
238
    protected function getMagento2Settings($helper, InputInterface $input, OutputInterface $output)
239
    {
240
        // todo add php 7 enabled box
241
        // $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...
242
243
        if ($this->_app === 'magento 2') {
244
            return $this->verifyAuth($helper, $input, $output);
245
        }
246
247
        return true;
248
    }
249
250
    /**
251
     * @param $helper
252
     * @param InputInterface $input
253
     * @param OutputInterface $output
254
     */
255
    protected function getVersionControlSettings($helper, InputInterface $input, OutputInterface $output)
256
    {
257
        $versionControl = new ConfirmationQuestion("Would you like to add your project to GIT? (yes/no) ", true);
258
        $versioning = $helper->ask($input, $output, $versionControl);
259
        if ($versioning) {
260
            $repoQuestion = new Question("Enter your full GitHub/BitBucket repo URL: ", '');
261
            $this->_repoUrl = strtolower($helper->ask($input, $output, $repoQuestion));
262
        }
263
    }
264
265
    /**
266
     * @param $helper
267
     * @param InputInterface $input
268
     * @param OutputInterface $output
269
     */
270
    protected function askForAuth($helper, InputInterface $input, OutputInterface $output)
271
    {
272
        $username = new Question("Please enter your Magento username (public key): ", '');
273
        $this->_m2Username = $helper->ask($input, $output, $username);
274
275
        $password = new Question("Please enter your Magento password (private key): ", '');
276
        $this->_m2Password = $helper->ask($input, $output, $password);
277
    }
278
279
    /**
280
     * @param $helper
281
     * @param InputInterface $input
282
     * @param OutputInterface $output
283
     * @return bool
284
     */
285
    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...
286
    {
287
        $authFile = $_SERVER['HOME'] . "/.composer/auth.json";
288
289
        $authObj = [];
290
        if (file_exists($authFile)) {
291
            $authJson = file_get_contents($authFile);
292
            $authObj = (array)json_decode($authJson);
293
294
            if (isset($authObj['http-basic']) && isset($authObj['http-basic']->{'repo.magento.com'})) {
295
                return true;
296
            }
297
        }
298
299
        $this->askForAuth($helper, $input, $output);
300
301
        $authObj['http-basic']['repo.magento.com']['username'] = $this->_m2Username;
302
        $authObj['http-basic']['repo.magento.com']['password'] = $this->_m2Password;
303
304
        $authJson = json_encode($authObj);
305
        return file_put_contents($authFile, $authJson);
306
    }
307
308
    /**
309
     * @param $helper
310
     * @param InputInterface $input
311
     * @param OutputInterface $output
312
     */
313
    protected function usePhp7($helper, InputInterface $input, OutputInterface $output)
314
    {
315
        if ($this->_app !== 'magento' && $this->_server !== 'apache') {
316
            $phpVerQuestion = new ChoiceQuestion(
317
                "Which version of PHP should be installed?",
318
                ['56', '7'],
319
                0
320
            );
321
            $this->_phpVer = $helper->ask($input, $output, $phpVerQuestion);
322
        }
323
    }
324
325
    protected function setVagrantBox()
326
    {
327
        $this->_box = self::BOX_PREFIX . $this->getOs() . "-$this->_server-php$this->_phpVer";
328
    }
329
}