Completed
Pull Request — epic/2.0.0 (#37)
by Steven
13:13
created

Options::usePhp7()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
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
/**
10
 * Class Options
11
 * @package Magestead\Helper
12
 */
13
class Options
14
{
15
//    const BOX_PREFIX = 'richdynamix/magestead-';
16
    const BOX_PREFIX = '';
17
    protected $_app = 'magento2';
18
    protected $_server;
19
    protected $_phpVer = '56';
20
    protected $_os = 'centos65';
21
    protected $_box;
22
    protected $_m2Username;
23
    protected $_m2Password;
24
    protected $_ipAddress;
25
    protected $_memorylimit;
26
    protected $_cpus;
27
    protected $_locale;
28
    protected $_currency;
29
    protected $_baseUrl;
30
    protected $_repoUrl = '';
31
32
    /**
33
     * Options constructor.
34
     * @param $helper
35
     * @param InputInterface $input
36
     * @param OutputInterface $output
37
     */
38
    public function __construct($helper, InputInterface $input, OutputInterface $output)
39
    {
40
        $this->setVagrantSettings($helper, $input, $output);
41
42
        $this->setServerConfig($helper, $input, $output);
43
44
        $this->setApplicationSettings($helper, $input, $output);
45
        $this->setMagento2Settings($helper, $input, $output);
46
        $this->setVersionControlSettings($helper, $input, $output);
47
48
        $this->setVagrantBox();
49
50
        print_r($this->getAllOptions());
51
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
52
53
    }
54
    /**
55
     * @return array
56
     */
57
    public function getAllOptions()
58
    {
59
        return [
60
          'app' => $this->_app,
61
          'server' => $this->_server,
62
          'phpver' => $this->_phpVer,
63
          'os' => $this->_os,
64
          'box' => $this->_box,
65
          'm2user' => $this->_m2Username,
66
          'm2pass' => $this->_m2Password,
67
          'repo_url' => $this->_repoUrl,
68
          'ip_address' => $this->_ipAddress,
69
          'cpus' => $this->_cpus,
70
          'memory_limit' => $this->_memorylimit,
71
          'locale' => $this->_locale,
72
          'default_currency' => $this->_currency,
73
          'base_url' => $this->_baseUrl,
74
        ];
75
    }
76
77
    /**
78
     * @param $helper
79
     * @param InputInterface $input
80
     * @param OutputInterface $output
81
     */
82
    protected function setVagrantSettings($helper, InputInterface $input, OutputInterface $output)
83
    {
84
        $output->writeln('<comment>Lets configure your project\'s VM</comment>');
85
86
        $ipQuestion = new Question("Configure the IP for your VM (192.168.47.47): ", '192.168.47.47');
87
        $this->_ipAddress = strtolower($helper->ask($input, $output, $ipQuestion));
88
89
        $cpuQuestion = new Question("How many CPU's would you like to use? (1): ", '1');
90
        $this->_cpus = strtolower($helper->ask($input, $output, $cpuQuestion));
91
92
        $memoryQuestion = new Question("Define the VM memory limit (2048): ", '2048');
93
        $this->_memorylimit = strtolower($helper->ask($input, $output, $memoryQuestion));
94
    }
95
96
    /**
97
     * @param $helper
98
     * @param InputInterface $input
99
     * @param OutputInterface $output
100
     */
101
    protected function setApplicationSettings($helper, InputInterface $input, OutputInterface $output)
102
    {
103
        $output->writeln('<comment>Lets configure your project\'s application</comment>');
104
        if ($this->_phpVer !== '70') {
105
            $appQuestion = new ChoiceQuestion(
106
                "Which application do you want to install?",
107
                ['Magento', 'Magento 2'],
108
                0
109
            );
110
111
            $this->_app = strtolower($helper->ask($input, $output, $appQuestion));
112
        }
113
        $baseUrlQuestion = new Question("Enter your application's base_url (magestead.dev): ", 'magestead.dev');
114
        $this->_baseUrl = strtolower($helper->ask($input, $output, $baseUrlQuestion));
115
116
        $currenyQuestion = new Question("Enter your application's default currency (GBP): ", 'GBP');
117
        $this->_currency = $helper->ask($input, $output, $currenyQuestion);
118
119
        $localeQuestion = new Question("Enter your application's default locale (en_GB): ", 'en_GB');
120
        $this->_locale = $helper->ask($input, $output, $localeQuestion);
121
    }
122
123
    /**
124
     * @param $helper
125
     * @param InputInterface $input
126
     * @param OutputInterface $output
127
     * @return bool
128
     */
129
    protected function setMagento2Settings($helper, InputInterface $input, OutputInterface $output)
130
    {
131
        if ($this->_app === 'magento2') {
132
            return $this->verifyAuth($helper, $input, $output);
133
        }
134
135
        return true;
136
    }
137
138
    /**
139
     * @param $helper
140
     * @param InputInterface $input
141
     * @param OutputInterface $output
142
     */
143
    protected function setVersionControlSettings($helper, InputInterface $input, OutputInterface $output)
144
    {
145
        $versionControl = new ConfirmationQuestion("Would you like to add your project to GIT? (no/yes) ", false);
146
        $versioning = $helper->ask($input, $output, $versionControl);
147
        if ($versioning) {
148
            $repoQuestion = new Question("Enter your full GitHub/BitBucket repo URL: ", '');
149
            $this->_repoUrl = strtolower($helper->ask($input, $output, $repoQuestion));
150
        }
151
    }
152
153
    /**
154
     * @param $helper
155
     * @param InputInterface $input
156
     * @param OutputInterface $output
157
     */
158
    protected function askForAuth($helper, InputInterface $input, OutputInterface $output)
159
    {
160
        $username = new Question("Please enter your Magento username (public key): ", '');
161
        $this->_m2Username = $helper->ask($input, $output, $username);
162
163
        $password = new Question("Please enter your Magento password (private key): ", '');
164
        $this->_m2Password = $helper->ask($input, $output, $password);
165
    }
166
167
    /**
168
     * @param $helper
169
     * @param InputInterface $input
170
     * @param OutputInterface $output
171
     * @return bool
172
     */
173
    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...
174
    {
175
        $authFile = $_SERVER['HOME'] . "/.composer/auth.json";
176
177
        $authObj = [];
178
        if (file_exists($authFile)) {
179
            $authJson = file_get_contents($authFile);
180
            $authObj = (array)json_decode($authJson);
181
182
            if (isset($authObj['http-basic']) && isset($authObj['http-basic']->{'repo.magento.com'})) {
183
                return true;
184
            }
185
        }
186
187
        $this->askForAuth($helper, $input, $output);
188
189
        $authObj['http-basic']['repo.magento.com']['username'] = $this->_m2Username;
190
        $authObj['http-basic']['repo.magento.com']['password'] = $this->_m2Password;
191
192
        $authJson = json_encode($authObj);
193
        return file_put_contents($authFile, $authJson);
194
    }
195
196
    /**
197
     * @param $helper
198
     * @param InputInterface $input
199
     * @param OutputInterface $output
200
     */
201
    protected function setPhp($helper, InputInterface $input, OutputInterface $output)
202
    {
203
        $output->writeln('<info>Keep in mind PHP7 is only available for Magento 2</info>');
204
        $phpVerQuestion = new ChoiceQuestion(
205
            "Which version of PHP should be installed?",
206
            ['56', '70'],
207
            0
208
        );
209
210
        $this->_phpVer = $helper->ask($input, $output, $phpVerQuestion);
211
    }
212
213
    /**
214
     * Set box name from concat user options
215
     */
216
    protected function setVagrantBox()
217
    {
218
        $this->_box = self::BOX_PREFIX . $this->_os . "-$this->_server-php$this->_phpVer";
219
    }
220
221
    /**
222
     * @param $helper
223
     * @param InputInterface $input
224
     * @param OutputInterface $output
225
     */
226
    protected function setServerConfig($helper, InputInterface $input, OutputInterface $output)
227
    {
228
        $output->writeln('<comment>Lets configure your server</comment>');
229
        $this->setOperatingSystem($helper, $input, $output);
230
        $this->setWebServer($helper, $input, $output);
231
        $this->setPhp($helper, $input, $output);
232
    }
233
234
    /**
235
     * @param $helper
236
     * @param InputInterface $input
237
     * @param OutputInterface $output
238
     */
239
    protected function setWebServer($helper, InputInterface $input, OutputInterface $output)
240
    {
241
        $serverQuestion = new ChoiceQuestion(
242
            "Which webserver would you like?",
243
            ['NGINX', 'Apache'],
244
            0
245
        );
246
        $this->_server = strtolower($helper->ask($input, $output, $serverQuestion));
247
    }
248
249
    /**
250
     * @param $helper
251
     * @param InputInterface $input
252
     * @param OutputInterface $output
253
     */
254
    protected function setOperatingSystem($helper, InputInterface $input, OutputInterface $output)
255
    {
256
        $osQuestion = new ChoiceQuestion(
257
            "Which OS would you like to install?",
258
            ['CentOS 6.5', 'Ubuntu 14.04'],
259
            0
260
        );
261
        $this->_os = str_replace(' ', '', str_replace('.', '', strtolower($helper->ask($input, $output, $osQuestion))));
262
    }
263
}