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

RewriteHtaccessFile   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 14.08 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 9
c 3
b 2
f 0
lcom 1
cbo 5
dl 10
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 10 31 6
A replaceHtaccessFile() 0 11 1
A _backupOriginalFile() 0 7 1
A _replaceContent() 0 6 1

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
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