Skip to content

Commit a5f892e

Browse files
author
Yurii Torbyk
committed
MAGETWO-34993: Refactor controllers from the list (Part1)
1 parent 8df6478 commit a5f892e

File tree

12 files changed

+218
-192
lines changed

12 files changed

+218
-192
lines changed

app/code/Magento/Backend/Controller/Adminhtml/Cache/CleanImages.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ class CleanImages extends \Magento\Backend\Controller\Adminhtml\Cache
1414
* Clean JS/css files cache
1515
*
1616
* @return \Magento\Backend\Model\View\Result\Redirect
17+
* @throws LocalizedException
1718
*/
1819
public function execute()
1920
{
20-
try {
21-
$this->_objectManager->create('Magento\Catalog\Model\Product\Image')->clearCache();
22-
$this->_eventManager->dispatch('clean_catalog_images_cache_after');
23-
$this->messageManager->addSuccess(__('The image cache was cleaned.'));
24-
} catch (LocalizedException $e) {
25-
$this->messageManager->addError($e->getMessage());
26-
} catch (\Exception $e) {
27-
$this->messageManager->addException($e, __('An error occurred while clearing the image cache.'));
28-
}
29-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
21+
$this->_objectManager->create('Magento\Catalog\Model\Product\Image')->clearCache();
22+
$this->_eventManager->dispatch('clean_catalog_images_cache_after');
23+
$this->messageManager->addSuccess(__('The image cache was cleaned.'));
24+
25+
return $this->getDefaultRedirect();
26+
}
27+
28+
/**
29+
* Redirect user to the previous or main page
30+
*
31+
* @return \Magento\Backend\Model\View\Result\Redirect
32+
*/
33+
public function getDefaultRedirect()
34+
{
3035
$resultRedirect = $this->resultRedirectFactory->create();
3136
return $resultRedirect->setPath('adminhtml/*');
3237
}

app/code/Magento/Backend/Controller/Adminhtml/Cache/CleanMedia.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ class CleanMedia extends \Magento\Backend\Controller\Adminhtml\Cache
1414
* Clean JS/css files cache
1515
*
1616
* @return \Magento\Backend\Model\View\Result\Redirect
17+
* @throws LocalizedException
1718
*/
1819
public function execute()
1920
{
20-
try {
21-
$this->_objectManager->get('Magento\Framework\View\Asset\MergeService')->cleanMergedJsCss();
22-
$this->_eventManager->dispatch('clean_media_cache_after');
23-
$this->messageManager->addSuccess(__('The JavaScript/CSS cache has been cleaned.'));
24-
} catch (LocalizedException $e) {
25-
$this->messageManager->addError($e->getMessage());
26-
} catch (\Exception $e) {
27-
$this->messageManager->addException($e, __('An error occurred while clearing the JavaScript/CSS cache.'));
28-
}
29-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
21+
$this->_objectManager->get('Magento\Framework\View\Asset\MergeService')->cleanMergedJsCss();
22+
$this->_eventManager->dispatch('clean_media_cache_after');
23+
$this->messageManager->addSuccess(__('The JavaScript/CSS cache has been cleaned.'));
24+
25+
return $this->getDefaultRedirect();
26+
}
27+
28+
/**
29+
* Redirect user to the previous or main page
30+
*
31+
* @return \Magento\Backend\Model\View\Result\Redirect
32+
*/
33+
public function getDefaultRedirect()
34+
{
3035
$resultRedirect = $this->resultRedirectFactory->create();
3136
return $resultRedirect->setPath('adminhtml/*');
3237
}

app/code/Magento/Backend/Controller/Adminhtml/Cache/MassDisable.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,34 @@ class MassDisable extends \Magento\Backend\Controller\Adminhtml\Cache
1717
*/
1818
public function execute()
1919
{
20-
try {
21-
$types = $this->getRequest()->getParam('types');
22-
$updatedTypes = 0;
23-
if (!is_array($types)) {
24-
$types = [];
25-
}
26-
$this->_validateTypes($types);
27-
foreach ($types as $code) {
28-
if ($this->_cacheState->isEnabled($code)) {
29-
$this->_cacheState->setEnabled($code, false);
30-
$updatedTypes++;
31-
}
32-
$this->_cacheTypeList->cleanType($code);
33-
}
34-
if ($updatedTypes > 0) {
35-
$this->_cacheState->persist();
36-
$this->messageManager->addSuccess(__("%1 cache type(s) disabled.", $updatedTypes));
20+
$types = $this->getRequest()->getParam('types');
21+
$updatedTypes = 0;
22+
if (!is_array($types)) {
23+
$types = [];
24+
}
25+
$this->_validateTypes($types);
26+
foreach ($types as $code) {
27+
if ($this->_cacheState->isEnabled($code)) {
28+
$this->_cacheState->setEnabled($code, false);
29+
$updatedTypes++;
3730
}
38-
} catch (LocalizedException $e) {
39-
$this->messageManager->addError($e->getMessage());
40-
} catch (\Exception $e) {
41-
$this->messageManager->addException($e, __('An error occurred while disabling cache.'));
31+
$this->_cacheTypeList->cleanType($code);
4232
}
43-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
33+
if ($updatedTypes > 0) {
34+
$this->_cacheState->persist();
35+
$this->messageManager->addSuccess(__("%1 cache type(s) disabled.", $updatedTypes));
36+
}
37+
38+
return $this->getDefaultRedirect();
39+
}
40+
41+
/**
42+
* Redirect user to the previous or main page
43+
*
44+
* @return \Magento\Backend\Model\View\Result\Redirect
45+
*/
46+
public function getDefaultRedirect()
47+
{
4448
$resultRedirect = $this->resultRedirectFactory->create();
4549
return $resultRedirect->setPath('adminhtml/*');
4650
}

app/code/Magento/Backend/Controller/Adminhtml/Cache/MassEnable.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,33 @@ class MassEnable extends \Magento\Backend\Controller\Adminhtml\Cache
1717
*/
1818
public function execute()
1919
{
20-
try {
21-
$types = $this->getRequest()->getParam('types');
22-
$updatedTypes = 0;
23-
if (!is_array($types)) {
24-
$types = [];
25-
}
26-
$this->_validateTypes($types);
27-
foreach ($types as $code) {
28-
if (!$this->_cacheState->isEnabled($code)) {
29-
$this->_cacheState->setEnabled($code, true);
30-
$updatedTypes++;
31-
}
32-
}
33-
if ($updatedTypes > 0) {
34-
$this->_cacheState->persist();
35-
$this->messageManager->addSuccess(__("%1 cache type(s) enabled.", $updatedTypes));
20+
$types = $this->getRequest()->getParam('types');
21+
$updatedTypes = 0;
22+
if (!is_array($types)) {
23+
$types = [];
24+
}
25+
$this->_validateTypes($types);
26+
foreach ($types as $code) {
27+
if (!$this->_cacheState->isEnabled($code)) {
28+
$this->_cacheState->setEnabled($code, true);
29+
$updatedTypes++;
3630
}
37-
} catch (LocalizedException $e) {
38-
$this->messageManager->addError($e->getMessage());
39-
} catch (\Exception $e) {
40-
$this->messageManager->addException($e, __('An error occurred while enabling cache.'));
4131
}
42-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
32+
if ($updatedTypes > 0) {
33+
$this->_cacheState->persist();
34+
$this->messageManager->addSuccess(__("%1 cache type(s) enabled.", $updatedTypes));
35+
}
36+
37+
return $this->getDefaultRedirect();
38+
}
39+
40+
/**
41+
* Redirect user to the previous or main page
42+
*
43+
* @return \Magento\Backend\Model\View\Result\Redirect
44+
*/
45+
public function getDefaultRedirect()
46+
{
4347
$resultRedirect = $this->resultRedirectFactory->create();
4448
return $resultRedirect->setPath('adminhtml/*');
4549
}

app/code/Magento/Backend/Controller/Adminhtml/Cache/MassRefresh.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,31 @@ class MassRefresh extends \Magento\Backend\Controller\Adminhtml\Cache
1717
*/
1818
public function execute()
1919
{
20-
try {
21-
$types = $this->getRequest()->getParam('types');
22-
$updatedTypes = 0;
23-
if (!is_array($types)) {
24-
$types = [];
25-
}
26-
$this->_validateTypes($types);
27-
foreach ($types as $type) {
28-
$this->_cacheTypeList->cleanType($type);
29-
$this->_eventManager->dispatch('adminhtml_cache_refresh_type', ['type' => $type]);
30-
$updatedTypes++;
31-
}
32-
if ($updatedTypes > 0) {
33-
$this->messageManager->addSuccess(__("%1 cache type(s) refreshed.", $updatedTypes));
34-
}
35-
} catch (LocalizedException $e) {
36-
$this->messageManager->addError($e->getMessage());
37-
} catch (\Exception $e) {
38-
$this->messageManager->addException($e, __('An error occurred while refreshing cache.'));
20+
$types = $this->getRequest()->getParam('types');
21+
$updatedTypes = 0;
22+
if (!is_array($types)) {
23+
$types = [];
3924
}
40-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
25+
$this->_validateTypes($types);
26+
foreach ($types as $type) {
27+
$this->_cacheTypeList->cleanType($type);
28+
$this->_eventManager->dispatch('adminhtml_cache_refresh_type', ['type' => $type]);
29+
$updatedTypes++;
30+
}
31+
if ($updatedTypes > 0) {
32+
$this->messageManager->addSuccess(__("%1 cache type(s) refreshed.", $updatedTypes));
33+
}
34+
35+
return $this->getDefaultRedirect();
36+
}
37+
38+
/**
39+
* Redirect user to the previous or main page
40+
*
41+
* @return \Magento\Backend\Model\View\Result\Redirect
42+
*/
43+
public function getDefaultRedirect()
44+
{
4145
$resultRedirect = $this->resultRedirectFactory->create();
4246
return $resultRedirect->setPath('adminhtml/*');
4347
}

app/code/Magento/Backend/Controller/Adminhtml/Dashboard/RefreshStatistics.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,23 @@ public function __construct(
3131
*/
3232
public function execute()
3333
{
34-
try {
35-
$collectionsNames = array_values($this->reportTypes);
36-
foreach ($collectionsNames as $collectionName) {
37-
$this->_objectManager->create($collectionName)->aggregate();
38-
}
39-
$this->messageManager->addSuccess(__('We updated lifetime statistic.'));
40-
} catch (\Exception $e) {
41-
$this->messageManager->addError(__('We can\'t refresh lifetime statistics.'));
42-
$this->logger->critical($e);
34+
$collectionsNames = array_values($this->reportTypes);
35+
foreach ($collectionsNames as $collectionName) {
36+
$this->_objectManager->create($collectionName)->aggregate();
4337
}
44-
return $this->resultRedirectFactory->create()->setPath('*/*');
38+
$this->messageManager->addSuccess(__('We updated lifetime statistic.'));
39+
40+
return $this->getDefaultRedirect();
41+
}
42+
43+
/**
44+
* Redirect user to the previous or main page
45+
*
46+
* @return \Magento\Backend\Model\View\Result\Redirect
47+
*/
48+
public function getDefaultRedirect()
49+
{
50+
$resultRedirect = $this->resultRedirectFactory->create();
51+
return $resultRedirect->setPath('*/*');
4552
}
4653
}

app/code/Magento/Backend/Controller/Adminhtml/System/Account/Save.php

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,15 @@
66
namespace Magento\Backend\Controller\Adminhtml\System\Account;
77

88
use Magento\Framework\Exception\AuthenticationException;
9+
use Magento\Framework\Exception\LocalizedException;
910

1011
class Save extends \Magento\Backend\Controller\Adminhtml\System\Account
1112
{
12-
/**
13-
* @var \Magento\Backend\Model\View\Result\RedirectFactory
14-
*/
15-
protected $resultRedirectFactory;
16-
17-
/**
18-
* @param \Magento\Backend\App\Action\Context $context
19-
* @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
20-
*/
21-
public function __construct(
22-
\Magento\Backend\App\Action\Context $context,
23-
\Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
24-
) {
25-
parent::__construct($context);
26-
$this->resultRedirectFactory = $resultRedirectFactory;
27-
}
28-
2913
/**
3014
* Saving edited user information
3115
*
3216
* @return \Magento\Backend\Model\View\Result\Redirect
17+
* @throws LocalizedException
3318
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
3419
*/
3520
public function execute()
@@ -42,17 +27,11 @@ public function execute()
4227
/** @var $user \Magento\User\Model\User */
4328
$user = $this->_objectManager->create('Magento\User\Model\User')->load($userId);
4429

45-
$user->setId(
46-
$userId
47-
)->setUsername(
48-
$this->getRequest()->getParam('username', false)
49-
)->setFirstname(
50-
$this->getRequest()->getParam('firstname', false)
51-
)->setLastname(
52-
$this->getRequest()->getParam('lastname', false)
53-
)->setEmail(
54-
strtolower($this->getRequest()->getParam('email', false))
55-
);
30+
$user->setId($userId)
31+
->setUsername($this->getRequest()->getParam('username', false))
32+
->setFirstname($this->getRequest()->getParam('firstname', false))
33+
->setLastname($this->getRequest()->getParam('lastname', false))
34+
->setEmail(strtolower($this->getRequest()->getParam('email', false)));
5635

5736
if ($this->_objectManager->get('Magento\Framework\Locale\Validator')->isValid($interfaceLocale)) {
5837
$user->setInterfaceLocale($interfaceLocale);
@@ -83,13 +62,19 @@ public function execute()
8362
if ($e->getMessage()) {
8463
$this->messageManager->addError($e->getMessage());
8564
}
86-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
87-
$this->messageManager->addError($e->getMessage());
88-
} catch (\Exception $e) {
89-
$this->messageManager->addError(__('An error occurred while saving account.'));
9065
}
91-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
66+
67+
return $this->getDefaultRedirect();
68+
}
69+
70+
/**
71+
* Redirect user to the previous or main page
72+
*
73+
* @return \Magento\Backend\Model\View\Result\Redirect
74+
*/
75+
public function getDefaultRedirect()
76+
{
9277
$resultRedirect = $this->resultRedirectFactory->create();
93-
return $resultRedirect->setPath("*/*/");
78+
return $resultRedirect->setPath('*/*');
9479
}
9580
}

app/code/Magento/Backend/Controller/Adminhtml/System/Store/DeleteGroupPost.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ public function execute()
3131
return $redirectResult->setPath('*/*/editGroup', ['group_id' => $itemId]);
3232
}
3333

34-
try {
35-
$model->delete();
36-
$this->messageManager->addSuccess(__('The store has been deleted.'));
37-
return $redirectResult->setPath('adminhtml/*/');
38-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
39-
$this->messageManager->addError($e->getMessage());
40-
} catch (\Exception $e) {
41-
$this->messageManager->addException($e, __('Unable to delete store. Please, try again later.'));
42-
}
43-
return $redirectResult->setPath('adminhtml/*/editGroup', ['group_id' => $itemId]);
34+
$model->delete();
35+
$this->messageManager->addSuccess(__('The store has been deleted.'));
36+
return $redirectResult->setPath('adminhtml/*/');
37+
}
38+
39+
/**
40+
* Redirect user to the previous or main page
41+
*
42+
* @return \Magento\Backend\Model\View\Result\Redirect
43+
*/
44+
public function getDefaultRedirect()
45+
{
46+
$resultRedirect = $this->resultRedirectFactory->create();
47+
return $resultRedirect->setPath(
48+
'adminhtml/*/editGroup',
49+
['group_id' => $this->getRequest()->getParam('item_id')]
50+
);
4451
}
4552
}

0 commit comments

Comments
 (0)