Completed
Push — develop ( 443f94...6d3b88 )
by Tom
04:20
created

RewriteHtaccessFile::hasFlagOrOptionalBoolOption()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 1
f 0
cc 3
eloc 7
nc 3
nop 2
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 void
11
     */
12
    public function execute()
13
    {
14
        if ($this->hasFlagOrOptionalBoolOption('useDefaultConfigParams')) {
15
            return;
16
        }
17
18
        $this->getCommand()->getApplication()->setAutoExit(false);
19
20
        $flag = $this->getOptionalBooleanOption('replaceHtaccessFile', 'Write BaseURL to .htaccess file?', false);
21
22
        if ($flag) {
23
            $this->replaceHtaccessFile();
24
        }
25
    }
26
27
    protected function replaceHtaccessFile()
28
    {
29
        $installationArgs = $this->config->getArray('installation_args');
30
        $baseUrl = $installationArgs['base-url'];
31
        $htaccessFile = $this->config->getString('installationFolder') . '/pub/.htaccess';
32
33
        $this->_backupOriginalFile($htaccessFile);
34
        $this->_replaceContent($htaccessFile, $baseUrl);
35
    }
36
37
    protected function _backupOriginalFile($htaccesFile)
38
    {
39
        copy(
40
            $htaccesFile,
41
            $htaccesFile . '.dist'
42
        );
43
    }
44
45
    /**
46
     * @param string $htaccessFile
47
     * @param string $baseUrl
48
     */
49
    protected function _replaceContent($htaccessFile, $baseUrl)
50
    {
51
        $content = file_get_contents($htaccessFile);
52
        $content = str_replace('#RewriteBase /magento/', 'RewriteBase ' . parse_url($baseUrl, PHP_URL_PATH), $content);
53
        file_put_contents($htaccessFile, $content);
54
    }
55
56
    /**
57
     * @param string $name of flag/option
58
     * @param bool $default value for flag/option if set but with no value
59
     * @return bool
60
     */
61
    private function hasFlagOrOptionalBoolOption($name, $default = true)
62
    {
63
        if (!$this->input->hasOption($name)) {
64
            return false;
65
        }
66
67
        $value = $this->input->getOption($name);
68
        if (null === $value) {
69
            return (bool) $default;
70
        }
71
72
        return (bool) $this->getCommand()->parseBoolOption($value);
73
    }
74
}
75