Magento2Project::configureRedis()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

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