DeleteCommand::execute()   C
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 50
rs 6.7272
cc 7
eloc 31
nc 13
nop 2
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(
28
                'scope',
29
                null,
30
                InputOption::VALUE_OPTIONAL,
31
                'The config value\'s scope (default, websites, stores)',
32
                'default'
33
            )
34
            ->addOption('scope-id', null, InputOption::VALUE_OPTIONAL, 'The config value\'s scope ID', '0')
35
            ->addOption('all', null, InputOption::VALUE_NONE, 'Delete all entries by path')
36
        ;
37
38
        $help = <<<HELP
39
To delete all entries if a path you can set the option --all.
40
HELP;
41
        $this->setHelp($help);
42
    }
43
44
    /**
45
     * @param \Symfony\Component\Console\Input\InputInterface $input
46
     * @param \Symfony\Component\Console\Output\OutputInterface $output
47
     * @return int|void
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $this->detectMagento($output, true);
52
        if (!$this->initMagento()) {
53
            return;
54
        }
55
56
        $this->_validateScopeParam($input->getOption('scope'));
57
        $scopeId = $this->_convertScopeIdParam($input->getOption('scope'), $input->getOption('scope-id'));
58
59
        $deleted = array();
60
61
        $path = $input->getArgument('path');
62
        $pathArray = array();
63
        if (strstr($path, '*')) {
64
            $collection = $this->getObjectManager()->get('\Magento\Config\Model\Resource\Config\Data\Collection');
65
            /* @var $collection \Magento\Config\Model\Resource\Config\Data\Collection */
66
67
            $searchPath = str_replace('*', '%', $path);
68
            $collection->addFieldToFilter('path', array('like' => $searchPath));
69
70
            if ($scopeId = $input->getOption('scope')) {
71
                $collection->addFieldToFilter(
72
                    'scope',
73
                    array(
74
                            'eq' => $scopeId
75
                    )
76
                );
77
            }
78
            $collection->addOrder('path', 'ASC');
79
80
            foreach ($collection as $item) {
81
                $pathArray[] = $item->getPath();
82
            }
83
        } else {
84
            $pathArray[] = $path;
85
        }
86
87
        $configWriter = $this->getConfigWriter();
88
        foreach ($pathArray as $pathToDelete) {
89
            $deleted = array_merge($deleted, $this->_deletePath($input, $configWriter, $pathToDelete, $scopeId));
90
        }
91
92
        if (count($deleted) > 0) {
93
            $this->getHelper('table')
94
                ->setHeaders(array('deleted path', 'scope', 'id'))
95
                ->setRows($deleted)
96
                ->render($output);
97
        }
98
    }
99
100
    /**
101
     * @param InputInterface $input
102
     * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
103
     * @param                $path
104
     * @param                $scopeId
105
     *
106
     * @return array
107
     */
108
    protected function _deletePath(
109
        InputInterface $input,
110
        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
111
        $path,
112
        $scopeId)
113
    {
114
        $deleted = array();
115
        if ($input->getOption('all')) {
116
            $storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManager');
117
118
            // Default
119
            $configWriter->delete(
120
                $path,
121
                'default',
122
                0
123
            );
124
125
            $deleted[] = array(
126
                'path'    => $path,
127
                'scope'   => 'default',
128
                'scopeId' => 0,
129
            );
130
131 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...
132
                $configWriter->delete(
133
                    $path,
134
                    'websites',
135
                    $website->getId()
136
                );
137
                $deleted[] = array(
138
                    'path'    => $path,
139
                    'scope'   => 'websites',
140
                    'scopeId' => $website->getId(),
141
                );
142
            }
143
144
            // Delete stores
145 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...
146
                $configWriter->delete(
147
                    $path,
148
                    'stores',
149
                    $store->getId()
150
                );
151
                $deleted[] = array(
152
                    'path'    => $path,
153
                    'scope'   => 'stores',
154
                    'scopeId' => $store->getId(),
155
                );
156
            }
157
        } else {
158
            $configWriter->delete(
159
                $path,
160
                $input->getOption('scope'),
161
                $scopeId
162
            );
163
164
            $deleted[] = array(
165
                'path'    => $path,
166
                'scope'   => $input->getOption('scope'),
167
                'scopeId' => $scopeId,
168
            );
169
        }
170
171
        return $deleted;
172
    }
173
}
174