Options::setVersionControlSettings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
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
17
    protected $_app = 'magento2';
18
    protected $_phpVer = '56';
19
    protected $_os = 'centos65';
20
    protected $_server;
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
    protected $installSampleData = false;
32
33
    /**
34
     * Options constructor.
35
     * @param $helper
36
     * @param InputInterface $input
37
     * @param OutputInterface $output
38
     * @param $project
39
     */
40
    public function __construct($helper, InputInterface $input, OutputInterface $output, $project)
41
    {
42
        $this->setVagrantSettings($helper, $input, $output);
43
44
        $this->setServerConfig($helper, $input, $output);
45
46
        $this->setApplicationSettings($helper, $input, $output, $project);
47
        $this->setMagento2Settings($helper, $input, $output);
48
        $this->setVersionControlSettings($helper, $input, $output);
49
50
        $this->setVagrantBox();
51
52
    }
53
    /**
54
     * @return array
55
     */
56
    public function getAllOptions()
57
    {
58
        return [
59
          'app' => $this->_app,
60
          'server' => $this->_server,
61
          'phpver' => $this->_phpVer,
62
          'os' => $this->_os,
63
          'box' => $this->_box,
64
          'm2user' => $this->_m2Username,
65
          'm2pass' => $this->_m2Password,
66
          'repo_url' => $this->_repoUrl,
67
          'ip_address' => $this->_ipAddress,
68
          'cpus' => $this->_cpus,
69
          'memory_limit' => $this->_memorylimit,
70
          'locale' => $this->_locale,
71
          'default_currency' => $this->_currency,
72
          'base_url' => $this->_baseUrl,
73
          'installSampleData' => $this->installSampleData,
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
     * @param $project
101
     */
102
    protected function setApplicationSettings($helper, InputInterface $input, OutputInterface $output, $project)
103
    {
104
        $output->writeln('<comment>Lets configure your project\'s application</comment>');
105
        $appQuestion = new ChoiceQuestion(
106
            "Which application do you want to install?",
107
            ['Magento', 'Magento2'],
108
            0
109
        );
110
111
        $this->_app = strtolower($helper->ask($input, $output, $appQuestion));
112
113
        $baseUrlQuestion = new Question("Enter your application's base_url ($project.dev): ", $project.'.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 boolean|integer
128
     */
129
    protected function setMagento2Settings($helper, InputInterface $input, OutputInterface $output)
130
    {
131
        if ($this->_app === 'magento2') {
132
            $this->installSampleData($helper, $input, $output);
133
            return $this->verifyAuth($helper, $input, $output);
134
        }
135
136
        return true;
137
    }
138
139
    /**
140
     * @param $helper
141
     * @param InputInterface $input
142
     * @param OutputInterface $output
143
     */
144
    protected function setVersionControlSettings($helper, InputInterface $input, OutputInterface $output)
145
    {
146
        $versionControl = new ConfirmationQuestion("Would you like to add your project to GIT? (no/yes) ", false);
147
        $versioning     = $helper->ask($input, $output, $versionControl);
148
        if ($versioning) {
149
            $repoQuestion   = new Question("Enter your full GitHub/BitBucket repo URL: ", '');
150
            $this->_repoUrl = strtolower($helper->ask($input, $output, $repoQuestion));
151
        }
152
    }
153
154
    protected function installSampleData($helper, InputInterface $input, OutputInterface $output)
155
    {
156
        $sampleInstall = new ConfirmationQuestion("Would you like to install sample data? (no/yes) ", false);
157
        $this->installSampleData = $helper->ask($input, $output, $sampleInstall);
158
    }
159
160
    /**
161
     * @param $helper
162
     * @param InputInterface $input
163
     * @param OutputInterface $output
164
     */
165
    protected function askForAuth($helper, InputInterface $input, OutputInterface $output)
166
    {
167
        $username          = new Question("Please enter your Magento username (public key): ", '');
168
        $this->_m2Username = $helper->ask($input, $output, $username);
169
170
        $password          = new Question("Please enter your Magento password (private key): ", '');
171
        $this->_m2Password = $helper->ask($input, $output, $password);
172
    }
173
174
    /**
175
     * @param $helper
176
     * @param InputInterface $input
177
     * @param OutputInterface $output
178
     * @return boolean|integer
179
     */
180
    protected function verifyAuth($helper, InputInterface $input, OutputInterface $output)
181
    {
182
        $composerHome = (new Config($output))->getComposerHomeDir();
183
        $authFile = $composerHome . "/auth.json";
184
185
        $authObj = [];
186
        if (file_exists($authFile)) {
187
            $authJson = file_get_contents($authFile);
188
            $authObj  = (array)json_decode($authJson, true);
189
190
            if (isset($authObj['http-basic']) && isset($authObj['http-basic']->{'repo.magento.com'})) {
191
                return true;
192
            }
193
        }
194
195
        $this->askForAuth($helper, $input, $output);
196
197
        $authObj['http-basic']['repo.magento.com']['username'] = $this->_m2Username;
198
        $authObj['http-basic']['repo.magento.com']['password'] = $this->_m2Password;
199
200
        $authJson = json_encode($authObj);
201
        return file_put_contents($authFile, $authJson);
202
    }
203
204
    /**
205
     * @param $helper
206
     * @param InputInterface $input
207
     * @param OutputInterface $output
208
     */
209
    protected function setPhp($helper, InputInterface $input, OutputInterface $output)
210
    {
211
        $phpVerQuestion = new ChoiceQuestion(
212
            "Which version of PHP should be installed?",
213
            ['56', '70'],
214
            0
215
        );
216
217
        $this->_phpVer = $helper->ask($input, $output, $phpVerQuestion);
218
    }
219
220
    /**
221
     * Set box name from concat user options
222
     */
223
    protected function setVagrantBox()
224
    {
225
        $this->_box = self::BOX_PREFIX . $this->_os . "-$this->_server-php$this->_phpVer";
226
    }
227
228
    /**
229
     * @param $helper
230
     * @param InputInterface $input
231
     * @param OutputInterface $output
232
     */
233
    protected function setServerConfig($helper, InputInterface $input, OutputInterface $output)
234
    {
235
        $output->writeln('<comment>Lets configure your server</comment>');
236
        $this->setOperatingSystem($helper, $input, $output);
237
        $this->setWebServer($helper, $input, $output);
238
        $this->setPhp($helper, $input, $output);
239
    }
240
241
    /**
242
     * @param $helper
243
     * @param InputInterface $input
244
     * @param OutputInterface $output
245
     */
246
    protected function setWebServer($helper, InputInterface $input, OutputInterface $output)
247
    {
248
        $serverQuestion = new ChoiceQuestion(
249
            "Which webserver would you like?",
250
            ['NGINX', 'Apache'],
251
            0
252
        );
253
254
        $this->_server = strtolower($helper->ask($input, $output, $serverQuestion));
255
    }
256
257
    /**
258
     * @param $helper
259
     * @param InputInterface $input
260
     * @param OutputInterface $output
261
     */
262
    protected function setOperatingSystem($helper, InputInterface $input, OutputInterface $output)
263
    {
264
        $osQuestion = new ChoiceQuestion(
265
            "Which OS would you like to install?",
266
            ['CentOS 6.5', 'Ubuntu 14'],
267
            0
268
        );
269
270
        $this->_os = str_replace(' ', '', str_replace('.', '', strtolower($helper->ask($input, $output, $osQuestion))));
271
    }
272
}
273