Completed
Push — develop ( a0c664...f8e6eb )
by Tom
04:27
created

RewriteHtaccessFile::execute()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 31
Code Lines 18

Duplication

Lines 10
Ratio 32.26 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 10
loc 31
rs 8.439
cc 6
eloc 18
nc 7
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Installer\SubCommand;
4
5
use N98\Magento\Command\SubCommand\AbstractSubCommand;
6
7
class RewriteHtaccessFile extends AbstractSubCommand
8
{
9
    /**
10
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
11
     */
12
    public function execute()
13
    {
14
        if ($this->input->getOption('useDefaultConfigParams') !== null
15
            || $this->input->getOption('replaceHtaccessFile') === null
16
        ) {
17
            return;
18
        }
19
20
        $this->getCommand()->getApplication()->setAutoExit(false);
21
        $dialog = $this->getCommand()->getHelper('dialog');
22
23
        $replaceHtaccessFile = false;
24
25 View Code Duplication
        if ($this->input->hasOption('replaceHtaccessFile')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
26
            $replaceHtaccessFile = $this->getCommand()->parseBoolOption($this->input->getOption('replaceHtaccessFile'));
27
        } elseif ($dialog->askConfirmation(
28
            $this->output,
29
            '<question>Write BaseURL to .htaccess file?</question> <comment>[n]</comment>: ',
30
            false
31
        )
32
        ) {
33
            $replaceHtaccessFile = true;
34
        }
35
36
        if (!$replaceHtaccessFile) {
37
            return;
38
        }
39
40
        $args = $this->config->getArray('installation_args');
41
        $this->replaceHtaccessFile($args['base-url']);
42
    }
43
44
    /**
45
     * @param string $baseUrl
46
     */
47
    protected function replaceHtaccessFile($baseUrl)
48
    {
49
        $htaccessFile = $this->config->getString('installationFolder')
50
                      . DIRECTORY_SEPARATOR
51
                      . 'pub'
52
                      . DIRECTORY_SEPARATOR
53
                      . '.htaccess';
54
55
        $this->_backupOriginalFile($htaccessFile);
56
        $this->_replaceContent($htaccessFile, $baseUrl);
57
    }
58
59
    protected function _backupOriginalFile($htaccesFile)
60
    {
61
        copy(
62
            $htaccesFile,
63
            $htaccesFile . '.dist'
64
        );
65
    }
66
67
    /**
68
     * @param string $htaccessFile
69
     * @param string $baseUrl
70
     */
71
    protected function _replaceContent($htaccessFile, $baseUrl)
72
    {
73
        $content = file_get_contents($htaccessFile);
74
        $content = str_replace('#RewriteBase /magento/', 'RewriteBase ' . parse_url($baseUrl, PHP_URL_PATH), $content);
75
        file_put_contents($htaccessFile, $content);
76
    }
77
}
78