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

src/N98/Magento/Command/Config/DeleteCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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()
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
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)
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) {
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) {
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