Skip to content

Commit 8f7e2ff

Browse files
committed
Added additional checks of storeId argument
1 parent 63c0461 commit 8f7e2ff

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

Console/Command/RegenerateUrlRewrites.php

+40-17
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function configure()
6868
new InputArgument(
6969
'storeId',
7070
InputArgument::OPTIONAL,
71-
'Store ID: 5'
71+
'5'
7272
)
7373
]);
7474
}
@@ -83,30 +83,37 @@ protected function execute(InputInterface $input, OutputInterface $output)
8383
{
8484
set_time_limit(0);
8585
$allStores = $this->getAllStoreIds();
86+
$storesList = [];
8687
$output->writeln('Regenerating of Url rewrites:');
8788

8889
// get store Id (if was set)
8990
$storeId = $input->getArgument('storeId');
9091

92+
// if store ID is not specified the re-generate for all stores
93+
if (is_null($storeId)) {
94+
$storesList = $allStores;
95+
}
9196
// we will re-generate URL only in this specific store (if it exists)
92-
if (!empty($storeId) && $storeId > 0) {
97+
elseif (strlen($storeId) && ctype_digit($storeId)) {
9398
if (isset($allStores[$storeId])) {
9499
$storesList = array(
95-
$storeId => $allStores[$storeId]
96-
);
100+
$storeId => $allStores[$storeId]
101+
);
97102
} else {
98-
$output->writeln('ERROR: store with this ID not exists.');
99-
$output->writeln('Finished');
103+
$this->displayError($output, 'ERROR: store with this ID not exists.');
100104
return;
101105
}
102106
}
103-
// otherwise we re-generate for all stores
107+
// disaply error if user set some incorrect value
104108
else {
105-
$storesList = $allStores;
109+
$this->displayError($output, 'ERROR: store ID should have a integer value.', true);
110+
return;
106111
}
107112

108113
// remove all current url rewrites
109-
$this->removeAllUrlRewrites($storesList);
114+
if (count($storesList) > 0) {
115+
$this->removeAllUrlRewrites($storesList);
116+
}
110117

111118
// set area code if needed
112119
try {
@@ -117,6 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
117124
}
118125

119126
foreach ($storesList as $storeId => $storeCode) {
127+
$output->writeln('');
120128
$output->write("[Store ID: {$storeId}, Store View code: {$storeCode}]:");
121129
$step = 0;
122130

@@ -146,11 +154,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
146154
$output->writeln($e->getMessage());
147155
}
148156
}
149-
$output->writeln('');
150157
}
151158

152159
$output->writeln('');
153-
160+
$output->writeln('');
154161
$output->writeln('Reindexation...');
155162
shell_exec('php bin/magento indexer:reindex');
156163

@@ -187,18 +194,34 @@ public function getAllStoreIds() {
187194
$result = [];
188195

189196
$sql = $this->_resource->getConnection()->select()
190-
->from($this->_resource->getTableName('store'), array('store_id', 'code'))
191-
->where('`code` <> ?', 'admin')
192-
->order('store_id', 'ASC');
197+
->from($this->_resource->getTableName('store'), array('store_id', 'code'))
198+
->order('store_id', 'ASC');
193199

194200
$queryResult = $this->_resource->getConnection()->fetchAll($sql);
195201

196202
foreach ($queryResult as $row) {
197-
if (isset($row['store_id']) && (int)$row['store_id'] > 0) {
198-
$result[(int)$row['store_id']] = $row['code'];
199-
}
203+
$result[(int)$row['store_id']] = $row['code'];
200204
}
201205

202206
return $result;
203207
}
208+
209+
/**
210+
* Display error message
211+
* @param OutputInterface $output
212+
* @param string $errorMsg
213+
* @param boolean $displayHint
214+
* @return void
215+
*/
216+
private function displayError(&$output, $errorMsg, $displayHint = false)
217+
{
218+
$output->writeln('');
219+
$output->writeln($errorMsg);
220+
221+
if ($displayHint) {
222+
$output->writeln('Correct command is: bin/magento ok:urlrewrites:regenerate 19');
223+
}
224+
225+
$output->writeln('Finished');
226+
}
204227
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Add into Magento 2 a CLI feature which allow to regenerate a Url rewrites of products/categories",
44
"keywords": ["magento", "magento2 extension", "magento 2 extension", "extension", "module", "magento2 module", "magento 2 module"],
55
"type": "magento2-module",
6-
"version": "1.0.3",
6+
"version": "1.0.4",
77
"homepage": "https://github.com/olegkoval/magento2-regenerate_url_rewrites",
88
"authors": [
99
{

etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
*/
1111
-->
1212
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
13-
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.0.3">
13+
<module name="OlegKoval_RegenerateUrlRewrites" setup_version="1.0.4">
1414
</module>
1515
</config>

0 commit comments

Comments
 (0)