Completed
Push — master ( 5cd237...36e8c5 )
by Tom
04:40
created

DeleteCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 155
Duplicated Lines 14.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 4 Features 1
Metric Value
wmc 14
c 6
b 4
f 1
lcom 1
cbo 2
dl 23
loc 155
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 23 28 1
B execute() 0 34 5
B _deletePath() 0 23 4
A expandPathPattern() 0 21 3
A deleteConfigEntry() 0 16 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\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 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...
13
    {
14
        $this
15
            ->setName('config:delete')
16
            ->setDescription('Deletes a store config item')
17
            ->addArgument('path', InputArgument::REQUIRED, 'The config path')
18
            ->addOption(
19
                'scope',
20
                null,
21
                InputOption::VALUE_OPTIONAL,
22
                'The config value\'s scope (default, websites, stores)',
23
                'default'
24
            )
25
            ->addOption('scope-id', null, InputOption::VALUE_OPTIONAL, 'The config value\'s scope ID', '0')
26
            ->addOption(
27
                'force',
28
                null,
29
                InputOption::VALUE_NONE,
30
                'Allow deletion of non-standard scope-id\'s for websites and stores'
31
            )
32
            ->addOption('all', null, InputOption::VALUE_NONE, 'Delete all entries by path')
33
        ;
34
35
        $help = <<<HELP
36
To delete all entries if a path you can set the option --all.
37
HELP;
38
        $this->setHelp($help);
39
    }
40
41
    /**
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     *
45
     * @return int|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->detectMagento($output, true);
50
51
        if (!$this->initMagento()) {
52
            return;
53
        }
54
55
        $deleted = array();
56
57
        $allowZeroScope = $input->getOption('force');
58
59
        $scope = $this->_validateScopeParam($input->getOption('scope'));
60
        $scopeId = $this->_convertScopeIdParam($scope, $input->getOption('scope-id'), $allowZeroScope);
61
62
        $path = $input->getArgument('path');
63
64
        if (false !== strstr($path, '*')) {
65
            $paths = $this->expandPathPattern($input, $path);
66
        } else {
67
            $paths = array($path);
68
        }
69
70
        foreach ($paths as $path) {
71
            $deleted = array_merge($deleted, $this->_deletePath($input, $path, $scopeId));
72
        }
73
74
        if (count($deleted) > 0) {
75
            $this->getHelper('table')
76
                ->setHeaders(array('deleted path', 'scope', 'id'))
77
                ->setRows($deleted)
78
                ->render($output);
79
        }
80
    }
81
82
    /**
83
     * @param InputInterface $input
84
     * @param string $path
85
     * @param string $scopeId
86
     *
87
     * @return array
88
     */
89
    protected function _deletePath(InputInterface $input, $path, $scopeId)
90
    {
91
        $deleted = array();
92
        $force = $input->getOption('force');
93
        if ($input->getOption('all')) {
94
            // Default
95
            $deleted[] = $this->deleteConfigEntry($path, 'default', 0);
96
97
            // Delete websites
98
            foreach (\Mage::app()->getWebsites($force) as $website) {
99
                $deleted[] = $this->deleteConfigEntry($path, 'websites', $website->getId());
100
            }
101
102
            // Delete stores
103
            foreach (\Mage::app()->getStores($force) as $store) {
104
                $deleted[] = $this->deleteConfigEntry($path, 'stores', $store->getId());
105
            }
106
        } else {
107
            $deleted[] = $this->deleteConfigEntry($path, $input->getOption('scope'), $scopeId);
108
        }
109
110
        return $deleted;
111
    }
112
113
    /**
114
     * @param string $pattern
115
     * @return array
116
     */
117
    private function expandPathPattern($input, $pattern)
118
    {
119
        $paths = array();
120
121
        /* @var $collection \Mage_Core_Model_Resource_Db_Collection_Abstract */
122
        $collection = $this->_getConfigDataModel()->getCollection();
123
124
        $likePattern = str_replace('*', '%', $pattern);
125
        $collection->addFieldToFilter('path', array('like' => $likePattern));
126
127
        if ($scope = $input->getOption('scope')) {
128
            $collection->addFieldToFilter('scope', array('eq' => $scope));
129
        }
130
        $collection->addOrder('path', 'ASC');
131
132
        foreach ($collection as $item) {
133
            $paths[] = $item->getPath();
134
        }
135
136
        return $paths;
137
    }
138
139
    /**
140
     * Delete concrete entry from config table specified by path, scope and scope-id
141
     *
142
     * @param string $path
143
     * @param string $scope
144
     * @param int $scopeId
145
     *
146
     * @return array
147
     */
148
    private function deleteConfigEntry($path, $scope, $scopeId)
149
    {
150
        $config = $this->_getConfigModel();
151
152
        $config->deleteConfig(
153
            $path,
154
            $scope,
155
            $scopeId
156
        );
157
158
        return array(
159
            'path'    => $path,
160
            'scope'   => $scope,
161
            'scopeId' => $scopeId,
162
        );
163
    }
164
}
165