Completed
Push — feature/add-cli-installers ( 1a65a6...26e1ec )
by Steven
13:36
created

MagentoProject::configureTestSuites()   B

Complexity

Conditions 3
Paths 9

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 19
nc 9
nop 3
1
<?php namespace Magestead\Installers;
2
3
use Magestead\Helper\HostsPluginChecker;
4
use Magestead\Service\Notification;
5
use Magestead\Service\VersionControl;
6
use Magestead\Command\ProcessCommand;
7
use Symfony\Component\Console\Helper\ProgressBar;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Yaml\Dumper;
11
use Symfony\Component\Yaml\Exception\ParseException;
12
use Symfony\Component\Yaml\Parser;
13
14
class MagentoProject
15
{
16
    /**
17
     * MagentoProject constructor.
18
     * @param array $options
19
     * @param array $config
20
     * @param $projectPath
21
     * @param OutputInterface $output
22
     */
23
    public function __construct(array $options, array $config, $projectPath, OutputInterface $output)
24
    {
25
        $output->writeln('<info>Installing Magento with Composer</info>');
26
        $this->composerInstall($projectPath, $output);
27
28
        $output->writeln('<info>Installing Magento Software</info>');
29
        $this->installMagento($config, $projectPath, $output);
30
31
        $this->configureTestSuites($options, $projectPath, $output);
32
33
        $output->writeln('<info>Finalising Setup</info>');
34
        $this->finaliseSetup($options, $projectPath, $output);
35
        $this->showCredentials($config, $output);
36
37
        Notification::send('Magento was successfully installed!');
38
    }
39
40
    /**
41
     * @param array $options
42
     * @param $projectPath
43
     * @param $output
44
     */
45
    protected function installMagento(array $options, $projectPath, OutputInterface $output)
46
    {
47
        $locale = $options['magestead']['apps']['mba_12345']['locale'];
48
        $db_name = $options['magestead']['apps']['mba_12345']['db_name'];
49
        $base_url = $options['magestead']['apps']['mba_12345']['base_url'];
50
        $default_currency = $options['magestead']['apps']['mba_12345']['default_currency'];
51
52
        $install = 'vagrant ssh -c \'cd /var/www/public; php -f install.php -- \
53
--license_agreement_accepted "yes" \
54
--locale "' . $locale . '" \
55
--timezone "Europe/London" \
56
--default_currency "' . $default_currency . '" \
57
--db_host "localhost" \
58
--db_name "' . $db_name . '" \
59
--db_user "magestead" \
60
--db_pass "vagrant" \
61
--session_save "db" \
62
--url "http://' . $base_url . '/" \
63
--use_rewrites "yes" \
64
--skip_url_validation "yes" \
65
--use_secure "no" \
66
--use_secure_admin "no" \
67
--secure_base_url "http://' . $base_url . '/" \
68
--admin_firstname "RichDynamix" \
69
--admin_lastname "Magestead" \
70
--admin_email "[email protected]" \
71
--admin_username "admin" \
72
--admin_password "password123"\' ';
73
74
        new ProcessCommand($install, $projectPath, $output);
75
76
        $this->configureRedis($projectPath);
77
        $this->setPermissions($projectPath, $output);
78
        $this->installMagerun($projectPath, $output);
79
    }
80
81
    /**
82
     * @param $projectPath
83
     * @param OutputInterface $output
84
     */
85
    protected function setPermissions($projectPath, OutputInterface $output)
86
    {
87
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find . -type f -exec chmod 400 {} \;\'';
88
        $output->writeln('<comment>Setting Files Permissions</comment>');
89
        new ProcessCommand($command, $projectPath, $output);
90
91
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find . -type d -exec chmod 500 {} \;\'';
92
        $output->writeln('<comment>Setting Folder Permissions</comment>');
93
        new ProcessCommand($command, $projectPath, $output);
94
95
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find var/ -type f -exec chmod 600 {} \;\'';
96
        $output->writeln('<comment>Setting "var" Files Permissions</comment>');
97
        new ProcessCommand($command, $projectPath, $output);
98
99
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find media/ -type f -exec chmod 600 {} \;\'';
100
        $output->writeln('<comment>Setting "media" Files Permissions</comment>');
101
        new ProcessCommand($command, $projectPath, $output);
102
103
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find var/ -type d -exec chmod 700 {} \;\'';
104
        $output->writeln('<comment>Setting "var" Folder Permissions</comment>');
105
        new ProcessCommand($command, $projectPath, $output);
106
107
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find media/ -type d -exec chmod 700 {} \;\'';
108
        $output->writeln('<comment>Setting "media" Folder Permissions</comment>');
109
        new ProcessCommand($command, $projectPath, $output);
110
111
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo chmod 700 includes;\'';
112
        $output->writeln('<comment>Setting "includes" Permissions</comment>');
113
        new ProcessCommand($command, $projectPath, $output);
114
115
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo chmod 600 includes/config.php;\'';
116
        $output->writeln('<comment>Setting "includes/config.php" Permissions</comment>');
117
        new ProcessCommand($command, $projectPath, $output);
118
    }
119
120
    /**
121
     * @param $projectPath
122
     * @param OutputInterface $output
123
     */
124
    protected function installMagerun($projectPath, OutputInterface $output)
125
    {
126
        $command = 'vagrant ssh -c \'cd /var/www/bin; sudo wget https://files.magerun.net/n98-magerun.phar;\'';
127
        $output->writeln('<info>Downloading Magerun</info>');
128
        new ProcessCommand($command, $projectPath, $output);
129
130
        $command = 'vagrant ssh -c \'cd /var/www/bin; sudo chmod +x ./n98-magerun.phar;\'';
131
        $output->writeln('<comment>Setting Magerun Permissions</comment>');
132
        new ProcessCommand($command, $projectPath, $output);
133
    }
134
135
    protected function finaliseSetup(array $options, $projectPath, OutputInterface $output)
136
    {
137
        $command = 'vagrant ssh -c \'cd /var/www/public; ../bin/n98-magerun.phar index:reindex:all;\'';
138
        $output->writeln('<comment>Reindexing Tables</comment>');
139
        new ProcessCommand($command, $projectPath, $output);
140
141
        $command = 'vagrant ssh -c \'cd /var/www/public; ../bin/n98-magerun.phar cache:enable;\'';
142
        $output->writeln('<comment>Enabling All Cache</comment>');
143
        new ProcessCommand($command, $projectPath, $output);
144
145
        $command = 'vagrant ssh -c \'cd /var/www/public; ../bin/n98-magerun.phar cache:flush;\'';
146
        $output->writeln('<comment>Flushing All Cache</comment>');
147
        new ProcessCommand($command, $projectPath, $output);
148
149
        $this->processVcs($options, $projectPath, $output);
150
151
        $command = 'vagrant ssh -c \'cd /var/www/public; ../bin/n98-magerun.phar sys:check;\'';
152
        $output->writeln('<comment>System Check</comment>');
153
        new ProcessCommand($command, $projectPath, $output);
154
    }
155
156
    /**
157
     * @param array $options
158
     * @param OutputInterface $output
159
     */
160 View Code Duplication
    protected function showCredentials(array $options, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $output->writeln('<info>SUCCESS: Magestead has finished installing Magento!</info>');
163
        $table = new Table($output);
164
        $table
165
            ->setHeaders(['Username', 'Password', 'Base URL'])
166
            ->setRows([
167
                ['admin', 'password123', $options['magestead']['apps']['mba_12345']['base_url']],
168
            ]);
169
        $table->render();
170
171
        HostsPluginChecker::verify($options, $output);
172
    }
173
174 View Code Duplication
    protected function processVcs(array $options, $projectPath, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        if (!empty($options['repo_url'])) {
177
            copy($projectPath . "/puphpet/magestead/magento/stubs/gitignore.tmp", $projectPath . "/.gitignore");
178
            return new VersionControl($options['repo_url'], $projectPath, $output);
179
        }
180
    }
181
182
    /**
183
     * @param $projectPath
184
     * @param OutputInterface $output
185
     */
186
    protected function composerInstall($projectPath, OutputInterface $output)
187
    {
188
        copy($projectPath . "/puphpet/magestead/magento/stubs/composer.tmp", $projectPath . "/composer.json");
189
        new ProcessCommand('composer install', $projectPath, $output);
190
    }
191
192
    /**
193
     * @param $projectPath
194
     */
195
    protected function configureRedis($projectPath)
196
    {
197
        $this->updateConfigXml($projectPath);
198
        $this->activateModule($projectPath);
199
    }
200
201
    /**
202
     * @param $projectPath
203
     */
204
    protected function updateConfigXml($projectPath)
205
    {
206
        $localFile = '/public/app/etc/local.xml';
207
        $localXml = file_get_contents($projectPath . $localFile);
208
209
        $config = new \SimpleXMLElement($localXml);
210
        $config->global[0]->redis_session[0]->host = '127.0.0.1';
211
        $config->global[0]->redis_session[0]->port = '6379';
212
        $config->global[0]->redis_session[0]->password = '';
213
        $config->global[0]->redis_session[0]->timeout = '2.5';
214
        $config->global[0]->redis_session[0]->persistent = '';
215
        $config->global[0]->redis_session[0]->db = '';
216
        $config->global[0]->redis_session[0]->compression_threshold = '2048';
217
        $config->global[0]->redis_session[0]->compression_lib = 'gzip';
218
        $config->global[0]->redis_session[0]->log_level = '1';
219
        $config->global[0]->redis_session[0]->max_concurrency = '6';
220
        $config->global[0]->redis_session[0]->break_after_frontend = '5';
221
        $config->global[0]->redis_session[0]->break_after_adminhtml = '30';
222
        $config->global[0]->redis_session[0]->first_lifetime = '600';
223
        $config->global[0]->redis_session[0]->bot_first_lifetime = '60';
224
        $config->global[0]->redis_session[0]->bot_lifetime = '7200';
225
        $config->global[0]->redis_session[0]->disable_locking = '0';
226
        $config->global[0]->redis_session[0]->min_lifetime = '60';
227
        $config->global[0]->redis_session[0]->max_lifetime = '2592000';
228
229
        file_put_contents($projectPath . $localFile, $config->asXML());
230
    }
231
232
    /**
233
     * @param $projectPath
234
     */
235
    protected function activateModule($projectPath)
236
    {
237
        $moduleFile = '/public/app/etc/modules/Cm_RedisSession.xml';
238
        $moduleXml = file_get_contents($projectPath . $moduleFile);
239
        $config = new \SimpleXMLElement($moduleXml);
240
        $config->modules[0]->Cm_RedisSession[0]->active = 'true';
241
        file_put_contents($projectPath . $moduleFile, $config->asXML());
242
    }
243
244
    /**
245
     * @param array $options
246
     * @param $projectPath
247
     * @param OutputInterface $output
248
     */
249
    protected function configureTestSuites(array $options, $projectPath, OutputInterface $output)
250
    {
251
        $progress = new ProgressBar($output, 2);
252
253
        $progress->start();
254
        copy($projectPath . "/puphpet/magestead/magento/stubs/phpspec.yml", $projectPath . "/phpspec.yml");
255
        $progress->advance();
256
257
        $yaml = new Parser();
258
        try {
259
            $behat = $yaml->parse(file_get_contents($projectPath . "/puphpet/magestead/magento/stubs/behat.yml"));
260
            $behat['default']['extensions']['MageTest\MagentoExtension\Extension']['base_url'] = $options['base_url'];
261
        } catch (ParseException $e) {
262
            $output->writeln('<error>Unable to parse the YAML config</error>');
263
        }
264
265
        $dumper = new Dumper();
266
        $yaml = $dumper->dump($behat, 6);
267
        try {
268
            file_put_contents($this->_projectPath . '/behat.yml', $yaml);
0 ignored issues
show
Bug introduced by
The property _projectPath 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...
269
            $progress->advance();
270
        } catch (\Exception $e) {
271
            $output->writeln('<error>Unable to write to the YAML file</error>');
272
        }
273
274
        $progress->finish();
275
    }
276
}