Completed
Push — master ( 1dd7d1...0c5b7b )
by Tom
09:54 queued 05:02
created

DeleteCommand::resolveScopeIds()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 13
nc 3
nop 3
1
<?php
2
3
namespace N98\Magento\Command\Config;
4
5
use Magento\Config\Model\ResourceModel\Config\Data\Collection;
6
use Magento\Framework\App\Config\Storage\WriterInterface;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class DeleteCommand extends AbstractConfigCommand
13
{
14
    /**
15
     * @var Collection
16
     */
17
    private $collection;
18
19
    /**
20
     * @var array
21
     */
22
    protected $_scopes = array(
23
        'default',
24
        'websites',
25
        'stores',
26
    );
27
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('config:delete')
32
            ->setDescription('Deletes a store config item')
33
            ->addArgument('path', InputArgument::REQUIRED, 'The config path')
34
            ->addOption(
35
                'scope',
36
                null,
37
                InputOption::VALUE_OPTIONAL,
38
                'The config value\'s scope (default, websites, stores)',
39
                'default'
40
            )
41
            ->addOption('scope-id', null, InputOption::VALUE_OPTIONAL, 'The config value\'s scope ID')
42
            ->addOption('all', null, InputOption::VALUE_NONE, 'Delete all entries by path')
43
        ;
44
45
        $help = <<<HELP
46
To delete all entries if a path you can set the option --all.
47
HELP;
48
        $this->setHelp($help);
49
    }
50
51
    /**
52
     * @param Collection $collection
53
     */
54
    public function inject(Collection $collection)
55
    {
56
        $this->collection = $collection;
57
    }
58
59
    /**
60
     * @param \Symfony\Component\Console\Input\InputInterface $input
61
     * @param \Symfony\Component\Console\Output\OutputInterface $output
62
     * @return int|void
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        $this->detectMagento($output, true);
67
        if (!$this->initMagento()) {
68
            return;
69
        }
70
71
        $this->_validateScopeParam($input->getOption('scope'));
72
        $scopeId = $this->_convertScopeIdParam($input->getOption('scope'), $input->getOption('scope-id'));
73
74
        $deleted = array();
75
76
        $paths = $this->resolvePaths($input->getArgument('path'), $scopeId);
77
78
        $configWriter = $this->getConfigWriter();
79
        foreach ($paths as $path) {
80
            $deleted = array_merge($deleted, $this->_deletePath($input, $configWriter, $path, $scopeId));
81
        }
82
83
        if (count($deleted) > 0) {
84
            $this->getHelper('table')
85
                ->setHeaders(array('deleted path', 'scope', 'id'))
86
                ->setRows($deleted)
87
                ->render($output);
88
        }
89
    }
90
91
    /**
92
     *
93
     */
94
    private function resolvePaths($path, $scopeId)
95
    {
96
        if (false === strstr($path, '*')) {
97
            return (array) $path;
98
        }
99
100
        $paths = array();
101
102
        $collection = clone $this->collection;
103
104
        $searchPath = str_replace('*', '%', $path);
105
        $collection->addFieldToFilter('path', array('like' => $searchPath));
106
107
        if ($scopeId) {
108
            $collection->addFieldToFilter('scope_id', $scopeId);
109
        }
110
111
        $collection->addOrder('path', 'ASC');
112
113
        foreach ($collection as $item) {
114
            $paths[] = $item->getPath();
115
        }
116
117
        $paths = array_unique($paths);
118
119
        return $paths;
120
    }
121
122
    /**
123
     * @param InputInterface $input
124
     * @param WriterInterface $configWriter
125
     * @param                $path
126
     * @param                $scopeId
127
     *
128
     * @return array
129
     */
130
    protected function _deletePath(
131
        InputInterface $input,
132
        WriterInterface $configWriter,
133
        $path,
134
        $scopeId
135
    ) {
136
        $deleted = array();
137
        if ($input->getOption('all')) {
138
            $storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManager');
139
140
            // Delete default
141
            $this->delete($configWriter, $deleted, $path, 'default', 0);
142
143
            $deleted[] = array(
144
                'path'    => $path,
145
                'scope'   => 'default',
146
                'scopeId' => 0,
147
            );
148
149
            // Delete websites
150
            foreach ($storeManager->getWebsites() as $website) {
151
                $this->delete($configWriter, $deleted, $path, 'websites', $website->getId());
152
            }
153
154
            // Delete stores
155
            foreach ($storeManager->getStores() as $store) {
156
                $this->delete($configWriter, $deleted, $path, 'stores', $store->getId());
157
            }
158
        } else {
159
            foreach ($this->resolveScopeIds($path, $input->getOption('scope'), $scopeId) as $item) {
160
                $this->delete($configWriter, $deleted, $path, $item[1], $item[2]);
161
            }
162
        }
163
164
        return $deleted;
165
    }
166
167
    private function delete(WriterInterface $configWriter, &$deleted, $path, $scope, $scopeId)
168
    {
169
        $configWriter->delete($path, $scope, $scopeId);
170
171
        $deleted[] = array(
172
            'path'    => $path,
173
            'scope'   => $scope,
174
            'scopeId' => $scopeId,
175
        );
176
    }
177
178
    /**
179
     * @param string $path
180
     * @param string $scope
181
     * @param int|null $scopeId
182
     *
183
     * @return array
184
     */
185
    private function resolveScopeIds($path, $scope, $scopeId)
186
    {
187
        $result = array();
188
189
        if ($scopeId !== null) {
190
            $result[] = array($path, $scope, $scopeId);
191
192
            return $result;
193
        }
194
195
        $collection = clone $this->collection;
196
197
        $collection->addFieldToFilter('path', array('eq' => $path));
198
        $collection->addFieldToFilter('scope', array('eq' => $scope));
199
        $collection->addOrder('scope_id', 'ASC');
200
201
        $collection->clear();
202
203
        foreach ($collection as $item) {
204
            $result[] = array($item->getPath(), $item->getScope(), $item->getScopeId());
205
        }
206
207
        return $result;
208
    }
209
}
210