Completed
Push — develop ( f14dbe...c520bf )
by Steven
03:04
created

Magento2Project::installSampleData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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