Completed
Push — feature/add-cli-installers ( 55f17a...115549 )
by Steven
13:41
created

Magento2Project   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 206
Duplicated Lines 24.76 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 10
c 2
b 0
f 2
lcom 0
cbo 6
dl 51
loc 206
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A composerInstall() 0 8 1
A addPredisPackage() 0 10 1
B installMagento() 31 31 1
A setPermissions() 0 15 1
A configureRedis() 0 53 1
A finaliseSetup() 0 12 1
A showCredentials() 13 13 1
A processVcs() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
class Magento2Project
11
{
12
    /**
13
     * Magento2Project constructor.
14
     * @param array $options
15
     * @param array $config
16
     * @param $projectPath
17
     * @param OutputInterface $output
18
     */
19
    public function __construct(array $options, array $config, $projectPath, OutputInterface $output)
20
    {
21
        $this->composerInstall($projectPath, $output);
22
        $this->installMagento($config, $projectPath, $output);
23
        $this->finaliseSetup($options, $projectPath, $output);
24
        $this->showCredentials($config, $output);
25
26
        Notification::send('Magento 2 was successfully installed!');
27
    }
28
29
    /**
30
     * @param $projectPath
31
     * @param OutputInterface $output
32
     */
33
    protected function composerInstall($projectPath, OutputInterface $output)
34
    {
35
        $output->writeln('<info>Installing Magento 2 with Composer</info>');
36
        $command = 'composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition public';
37
        new ProcessCommand($command, $projectPath, $output);
38
39
//        $this->addPredisPackage($projectPath, $output);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
    }
41
42
    /**
43
     * @param $projectPath
44
     * @param OutputInterface $output
45
     */
46
    protected function addPredisPackage($projectPath, OutputInterface $output)
47
    {
48
        chdir($projectPath.'/public');
49
50
        echo getcwd();
51
52
        $output->writeln('<comment>Installing Predis package</comment>');
53
        $command = 'composer require predis/predis;';
54
        new ProcessCommand($command, $projectPath, $output);
55
    }
56
57
    /**
58
     * @param array $options
59
     * @param $projectPath
60
     * @param OutputInterface $output
61
     */
62 View Code Duplication
    protected function installMagento(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...
63
    {
64
        $this->setPermissions($projectPath, $output);
65
66
        $output->writeln('<info>Installing Magento 2 Software</info>');
67
        $locale = $options['magestead']['apps']['mba_12345']['locale'];
68
        $db_name = $options['magestead']['apps']['mba_12345']['db_name'];
69
        $base_url = $options['magestead']['apps']['mba_12345']['base_url'];
70
        $default_currency = $options['magestead']['apps']['mba_12345']['default_currency'];
71
72
        $install = 'vagrant ssh -c \'cd /var/www/public; bin/magento setup:install --base-url=http://'.$base_url.'/ \
73
--db-host=localhost \
74
--db-name='.$db_name.' \
75
--db-user=magestead \
76
--db-password=vagrant \
77
--admin-firstname=RichDynamix \
78
--admin-lastname=Magestead \
79
[email protected] \
80
--admin-user=admin \
81
--admin-password=password123 \
82
--language='.$locale.' \
83
--currency='.$default_currency.' \
84
--timezone=Europe/London \
85
--use-rewrites=1 \
86
--backend-frontname=admin \
87
--session-save=db \'';
88
89
        new ProcessCommand($install, $projectPath, $output);
90
91
        $this->configureRedis($projectPath, $output);
92
    }
93
94
    /**
95
     * @param $projectPath
96
     * @param OutputInterface $output
97
     */
98
    protected function setPermissions($projectPath, OutputInterface $output)
99
    {
100
        $output->writeln('<info>Setting Permissions</info>');
101
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find . -type d -exec chmod 700 {} \;\'';
102
        new ProcessCommand($command, $projectPath, $output);
103
        $output->writeln('<comment>Folder Permissions Set</comment>');
104
105
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo find . -type f -exec chmod 600 {} \;\'';
106
        new ProcessCommand($command, $projectPath, $output);
107
        $output->writeln('<comment>File Permissions Set</comment>');
108
109
        $command = 'vagrant ssh -c \'cd /var/www/public; sudo chmod +x bin/magento\'';
110
        new ProcessCommand($command, $projectPath, $output);
111
        $output->writeln('<comment>bin/magento Permissions Set</comment>');
112
    }
113
114
    /**
115
     * @param $projectPath
116
     * @param OutputInterface $output
117
     */
118
    protected function configureRedis($projectPath, OutputInterface $output)
119
    {
120
        $output->writeln('<comment>Configuring Redis Cache</comment>');
121
        $file = "$projectPath/public/app/etc/env.php";
122
        $env = include $file;
123
124
        $env['cache'] =
125
            array (
126
                'frontend' =>
127
                    array (
128
                        'default' =>
129
                            array (
130
                                'backend' => 'Cm_Cache_Backend_Redis',
131
                                'backend_options' =>
132
                                    array (
133
                                        'server' => '127.0.0.1',
134
                                        'port' => '6379',
135
                                        'persistent' => '',
136
                                        'database' => '0',
137
                                        'force_standalone' => '0',
138
                                        'connect_retries' => '1',
139
                                        'read_timeout' => '10',
140
                                        'automatic_cleaning_factor' => '0',
141
                                        'compress_data' => '1',
142
                                        'compress_tags' => '1',
143
                                        'compress_threshold' => '20480',
144
                                        'compression_lib' => 'gzip',
145
                                    )
146
                            ),
147
                        'page_cache' =>
148
                            array (
149
                                'backend' => 'Cm_Cache_Backend_Redis',
150
                                'backend_options' =>
151
                                    array (
152
                                        'server' => '127.0.0.1',
153
                                        'port' => '6379',
154
                                        'persistent' => '',
155
                                        'database' => '1',
156
                                        'force_standalone' => '0',
157
                                        'connect_retries' => '1',
158
                                        'read_timeout' => '10',
159
                                        'automatic_cleaning_factor' => '0',
160
                                        'compress_data' => '0',
161
                                        'compress_tags' => '1',
162
                                        'compress_threshold' => '20480',
163
                                        'compression_lib' => 'gzip',
164
                                    ),
165
                            ),
166
                    )
167
            );
168
169
        file_put_contents($file, "<?php \n \n return ".var_export($env,true).";");
170
    }
171
172
    /**
173
     * @param array $options
174
     * @param $projectPath
175
     * @param OutputInterface $output
176
     */
177
    protected function finaliseSetup(array $options, $projectPath, OutputInterface $output)
178
    {
179
        $command = 'vagrant ssh -c \'cd /var/www/public; bin/magento cache:flush;\'';
180
        $output->writeln('<comment>Flushing All Cache</comment>');
181
        new ProcessCommand($command, $projectPath, $output);
182
183
        $command = 'vagrant ssh -c \'cd /var/www/public; bin/magento indexer:reindex; \'';
184
        $output->writeln('<comment>Reindexing Tables</comment>');
185
        new ProcessCommand($command, $projectPath, $output);
186
187
        $this->processVcs($options, $projectPath, $output);
188
    }
189
190
    /**
191
     * @param array $options
192
     * @param OutputInterface $output
193
     */
194 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...
195
    {
196
        $output->writeln('<info>SUCCESS: Magestead has finished installing Magento 2!</info>');
197
        $table = new Table($output);
198
        $table
199
            ->setHeaders(['Username', 'Password', 'Base URL', 'Admin URI'])
200
            ->setRows([
201
                ['admin', 'password123', $options['magestead']['apps']['mba_12345']['base_url'], 'admin'],
202
            ]);
203
        $table->render();
204
205
        HostsPluginChecker::verify($options, $output);
206
    }
207
208 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...
209
    {
210
        if (!empty($options['repo_url'])) {
211
            copy($projectPath . "/puphpet/magestead/magento2/stubs/gitignore.tmp", $projectPath . "/.gitignore");
212
            return new VersionControl($options['repo_url'], $projectPath, $output);
213
        }
214
    }
215
}