Completed
Pull Request — master (#177)
by
unknown
04:33
created

DeleteCommand::_deletePath()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 68
Code Lines 44

Duplication

Lines 24
Ratio 35.29 %
Metric Value
dl 24
loc 68
rs 8.7864
cc 4
eloc 44
nc 2
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace N98\Magento\Command\Config;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class DeleteCommand extends AbstractConfigCommand
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $_scopes = array(
16
        'default',
17
        'websites',
18
        'stores',
19
    );
20
21 View Code Duplication
    protected function configure()
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...
22
    {
23
        $this
24
            ->setName('config:delete')
25
            ->setDescription('Deletes a store config item')
26
            ->addArgument('path', InputArgument::REQUIRED, 'The config path')
27
            ->addOption('scope', null, InputOption::VALUE_OPTIONAL, 'The config value\'s scope (default, websites, stores)', 'default')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
28
            ->addOption('scope-id', null, InputOption::VALUE_OPTIONAL, 'The config value\'s scope ID', '0')
29
            ->addOption('all', null, InputOption::VALUE_NONE, 'Delete all entries by path')
30
        ;
31
32
        $help = <<<HELP
33
To delete all entries if a path you can set the option --all.
34
HELP;
35
        $this->setHelp($help);
36
    }
37
38
    /**
39
     * @param \Symfony\Component\Console\Input\InputInterface $input
40
     * @param \Symfony\Component\Console\Output\OutputInterface $output
41
     * @return int|void
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $this->detectMagento($output, true);
46
        if ($this->initMagento()) {
47
48
            $this->_validateScopeParam($input->getOption('scope'));
49
            $scopeId = $this->_convertScopeIdParam($input->getOption('scope'), $input->getOption('scope-id'));
50
51
            $deleted = array();
52
53
            $path = $input->getArgument('path');
54
            $pathArray = array();
55
            if (strstr($path, '*')) {
56
                $collection = $this->getObjectManager()->get('\Magento\Config\Model\Resource\Config\Data\Collection');
57
                /* @var $collection \Magento\Config\Model\Resource\Config\Data\Collection */
58
59
                $searchPath = str_replace('*', '%', $path);
60
                $collection->addFieldToFilter('path', array('like' => $searchPath));
61
62
                if ($scopeId = $input->getOption('scope')) {
63
                    $collection->addFieldToFilter(
64
                        'scope',
65
                        array(
66
                             'eq' => $scopeId
67
                        )
68
                    );
69
                }
70
                $collection->addOrder('path', 'ASC');
71
72
                foreach ($collection as $item) {
73
                    $pathArray[] = $item->getPath();
74
                }
75
            } else {
76
                $pathArray[] = $path;
77
            }
78
79
            $configWriter = $this->getConfigWriter();
80
            foreach ($pathArray as $pathToDelete) {
81
                $deleted = array_merge($deleted, $this->_deletePath($input, $configWriter, $pathToDelete, $scopeId));
82
            }
83
        }
84
85
        if (count($deleted) > 0) {
86
            $this->getHelper('table')
87
                ->setHeaders(array('deleted path', 'scope', 'id'))
88
                ->setRows($deleted)
0 ignored issues
show
Bug introduced by
The variable $deleted does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
89
                ->render($output);
90
        }
91
    }
92
93
    /**
94
     * @param InputInterface $input
95
     * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
96
     * @param                $path
97
     * @param                $scopeId
98
     *
99
     * @return array
100
     */
101
    protected function _deletePath(
102
        InputInterface $input,
103
        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
104
        $path,
105
        $scopeId)
106
    {
107
        $deleted = array();
108
        if ($input->getOption('all')) {
109
110
            $storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManager');
111
112
            // Default
113
            $configWriter->delete(
114
                $path,
115
                'default',
116
                0
117
            );
118
119
            $deleted[] = array(
120
                'path'    => $path,
121
                'scope'   => 'default',
122
                'scopeId' => 0,
123
            );
124
125 View Code Duplication
            foreach ($storeManager->getWebsites() as $website) {
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...
126
                $configWriter->delete(
127
                    $path,
128
                    'websites',
129
                    $website->getId()
130
                );
131
                $deleted[] = array(
132
                    'path'    => $path,
133
                    'scope'   => 'websites',
134
                    'scopeId' => $website->getId(),
135
                );
136
            }
137
138
            // Delete stores
139 View Code Duplication
            foreach ($storeManager->getStores() as $store) {
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...
140
                $configWriter->delete(
141
                    $path,
142
                    'stores',
143
                    $store->getId()
144
                );
145
                $deleted[] = array(
146
                    'path'    => $path,
147
                    'scope'   => 'stores',
148
                    'scopeId' => $store->getId(),
149
                );
150
            }
151
152
        } else {
153
            $configWriter->delete(
154
                $path,
155
                $input->getOption('scope'),
156
                $scopeId
157
            );
158
159
            $deleted[] = array(
160
                'path'    => $path,
161
                'scope'   => $input->getOption('scope'),
162
                'scopeId' => $scopeId,
163
            );
164
165
        }
166
167
        return $deleted;
168
    }
169
}
170