Completed
Push — develop ( c6a956...40134e )
by Steven
9s
created

Magento2Project::addBehatPackage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php namespace Magestead\Installers;
2
3
use Magestead\Command\ProcessCommand;
4
use Magestead\Helper\HostsPluginChecker;
5
use Magestead\Service\Notification;
6
use Magestead\Service\VersionControl;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class Magento2Project
12
 * @package Magestead\Installers
13
 */
14
class Magento2Project
15
{
16
    /**
17
     * Magento2Project 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
        $this->composerInstall($projectPath, $output);
26
        $this->installMagento($config, $projectPath, $output);
27
        $this->finaliseSetup($options, $projectPath, $output);
28
        $this->showCredentials($config, $output);
29
30
        Notification::send('Magento 2 was successfully installed!');
31
    }
32
33
    /**
34
     * @param $projectPath
35
     * @param OutputInterface $output
36
     */
37
    protected function composerInstall($projectPath, OutputInterface $output)
38
    {
39
        $output->writeln('<info>Installing Magento 2 with Composer</info>');
40
        $command = 'composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition public';
41
        new ProcessCommand($command, $projectPath, $output);
42
43
        $this->setComposerBinDir($projectPath);
44
        $this->addPhpSpecPackage($projectPath, $output);
45
        $this->addBehatPackage($projectPath, $output);
46
    }
47
48
    /**
49
     * @param $projectPath
50
     * @param OutputInterface $output
51
     */
52 View Code Duplication
    protected function addPhpSpecPackage($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...
53
    {
54
        $output->writeln('<comment>Installing PHPSpec</comment>');
55
        $command = 'cd '.$projectPath.'/public; composer require phpspec/phpspec --dev;';
56
        new ProcessCommand($command, $projectPath, $output);
57
58
        $this->setPhpSpecPermissions($projectPath, $output);
59
    }
60
61
    /**
62
     * @param $projectPath
63
     * @param OutputInterface $output
64
     */
65 View Code Duplication
    protected function addBehatPackage($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...
66
    {
67
        $output->writeln('<comment>Installing Behat</comment>');
68
        $command = 'cd '.$projectPath.'/public; composer require behat/behat --dev;';
69
        new ProcessCommand($command, $projectPath, $output);
70
71
        $this->setBehatPermissions($projectPath, $output);
72
    }
73
74
    /**
75
     * @param array $options
76
     * @param $projectPath
77
     * @param OutputInterface $output
78
     */
79
    protected function installMagento(array $options, $projectPath, OutputInterface $output)
80
    {
81
        $this->setPermissions($projectPath, $output);
82
83
        $output->writeln('<info>Installing Magento 2 Software</info>');
84
        $locale           = $options['magestead']['apps']['mba_12345']['locale'];
85
        $db_name          = $options['magestead']['apps']['mba_12345']['db_name'];
86
        $base_url         = $options['magestead']['apps']['mba_12345']['base_url'];
87
        $default_currency = $options['magestead']['apps']['mba_12345']['default_currency'];
88
89
        $install = 'vagrant ssh -c \'cd /var/www/public; bin/magento setup:install --base-url=http://'.$base_url.'/ \
90
--db-host=localhost \
91
--db-name='.$db_name.' \
92
--db-user=magestead \
93
--db-password=vagrant \
94
--admin-firstname=RichDynamix \
95
--admin-lastname=Magestead \
96
[email protected] \
97
--admin-user=admin \
98
--admin-password=password123 \
99
--language='.$locale.' \
100
--currency='.$default_currency.' \
101
--timezone=Europe/London \
102
--use-rewrites=1 \
103
--backend-frontname=admin \
104
--session-save=db \'';
105
106
        new ProcessCommand($install, $projectPath, $output);
107
    }
108
109
    /**
110
     * @param $projectPath
111
     * @param OutputInterface $output
112
     */
113
    protected function setPermissions($projectPath, OutputInterface $output)
114
    {
115
        $output->writeln('<info>Setting Permissions</info>');
116
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find . -type d -exec chmod 700 {} \;\'';
117
        new ProcessCommand($command, $projectPath, $output);
118
        $output->writeln('<comment>Folder Permissions Set</comment>');
119
120
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find . -type f -exec chmod 600 {} \;\'';
121
        new ProcessCommand($command, $projectPath, $output);
122
        $output->writeln('<comment>File Permissions Set</comment>');
123
124
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo chmod +x bin/magento; sudo chmod 755 bin/phpspec; sudo chmod 755 bin/behat;\'';
125
        new ProcessCommand($command, $projectPath, $output);
126
        $output->writeln('<comment>bin/magento Permissions Set</comment>');
127
    }
128
129
    /**
130
     * @param $projectPath
131
     * @param OutputInterface $output
132
     */
133
    protected function configureRedis($projectPath, OutputInterface $output)
134
    {
135
        $output->writeln('<comment>Configuring Redis Cache</comment>');
136
        $file = "$projectPath/public/app/etc/env.php";
137
        $env  = include $file;
138
139
        $env['cache'] = [
140
            'frontend' => [
141
                'default' => [
142
                    'backend' => 'Cm_Cache_Backend_Redis',
143
                    'backend_options' => [
144
                        'server' => '127.0.0.1',
145
                        'port' => '6379',
146
                        'persistent' => '',
147
                        'database' => '0',
148
                        'force_standalone' => '0',
149
                        'connect_retries' => '1',
150
                        'read_timeout' => '10',
151
                        'automatic_cleaning_factor' => '0',
152
                        'compress_data' => '1',
153
                        'compress_tags' => '1',
154
                        'compress_threshold' => '20480',
155
                        'compression_lib' => 'gzip',
156
                    ]
157
                ],
158
                'page_cache' => [
159
                    'backend' => 'Cm_Cache_Backend_Redis',
160
                    'backend_options' => [
161
                        'server' => '127.0.0.1',
162
                        'port' => '6379',
163
                        'persistent' => '',
164
                        'database' => '1',
165
                        'force_standalone' => '0',
166
                        'connect_retries' => '1',
167
                        'read_timeout' => '10',
168
                        'automatic_cleaning_factor' => '0',
169
                        'compress_data' => '0',
170
                        'compress_tags' => '1',
171
                        'compress_threshold' => '20480',
172
                        'compression_lib' => 'gzip',
173
                    ],
174
                ],
175
            ],
176
        ];
177
178
        file_put_contents($file, "<?php \n \n return ".var_export($env,true).";");
179
    }
180
181
    /**
182
     * @param array $options
183
     * @param $projectPath
184
     * @param OutputInterface $output
185
     */
186
    protected function finaliseSetup(array $options, $projectPath, OutputInterface $output)
187
    {
188
        $command = 'vagrant ssh -c \'cd /var/www/public; bin/magento indexer:reindex; \'';
189
        $output->writeln('<comment>Reindexing Tables</comment>');
190
        new ProcessCommand($command, $projectPath, $output);
191
192
        $command = 'vagrant ssh -c \'cd /var/www/public; bin/magento cache:flush;\'';
193
        $output->writeln('<comment>Flushing All Cache</comment>');
194
        new ProcessCommand($command, $projectPath, $output);
195
196
        $this->configureRedis($projectPath, $output);
197
        $this->processVcs($options, $projectPath, $output);
198
    }
199
200
    /**
201
     * @param array $options
202
     * @param OutputInterface $output
203
     */
204 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...
205
    {
206
        $output->writeln('<info>SUCCESS: Magestead has finished installing Magento 2!</info>');
207
        $table = new Table($output);
208
        $table
209
            ->setHeaders(['Username', 'Password', 'Base URL', 'Admin URI'])
210
            ->setRows([
211
                ['admin', 'password123', $options['magestead']['apps']['mba_12345']['base_url'], 'admin'],
212
            ]);
213
        $table->render();
214
215
        HostsPluginChecker::verify($options, $output);
216
    }
217
218
    /**
219
     * @param array $options
220
     * @param $projectPath
221
     * @param OutputInterface $output
222
     * @return VersionControl|null
223
     */
224 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...
225
    {
226
        if (!empty($options['repo_url'])) {
227
            copy($projectPath . "/puphpet/magestead/magento2/stubs/gitignore.tmp", $projectPath . "/.gitignore");
228
            return new VersionControl($options['repo_url'], $projectPath, $output);
229
        }
230
    }
231
232
    /**
233
     * @param $projectPath
234
     */
235
    protected function setComposerBinDir($projectPath)
236
    {
237
        $file     = "$projectPath/public/composer.json";
238
        $composer = json_decode(file_get_contents($file), true);
239
240
        $composer['config']['bin-dir'] = 'bin';
241
        file_put_contents($file, json_encode($composer));
242
    }
243
244
    /**
245
     * @param $projectPath
246
     * @param OutputInterface $output
247
     */
248
    protected function setPhpSpecPermissions($projectPath, OutputInterface $output)
249
    {
250
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo chmod 755 bin/phpspec; bin/phpspec run\'';
251
        new ProcessCommand($command, $projectPath, $output);
252
    }
253
254
    /**
255
     * @param $projectPath
256
     * @param OutputInterface $output
257
     */
258
    protected function setBehatPermissions($projectPath, OutputInterface $output)
259
    {
260
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo chmod 755 bin/behat; bin/behat --init\'';
261
        new ProcessCommand($command, $projectPath, $output);
262
    }
263
}