Completed
Push — master ( 282359...338c1f )
by Steven
02:58
created

Magento2Project::copyAuthFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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, $options, $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->copyAuthFile($projectPath);
44
        $this->setComposerBinDir($projectPath);
45
        $this->addPhpSpecPackage($projectPath, $output);
46
        $this->addBehatPackage($projectPath, $output);
47
    }
48
49
    /**
50
     * @param $destination
51
     * @return bool
52
     */
53
    protected function copyAuthFile($destination)
0 ignored issues
show
Coding Style introduced by
copyAuthFile uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

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