Skip to content

Commit 8c352a7

Browse files
author
vpaladiychuk
committed
MAGETWO-34995: Refactor controllers from the list (Part2)
1 parent b39b013 commit 8c352a7

File tree

5 files changed

+66
-145
lines changed

5 files changed

+66
-145
lines changed

app/code/Magento/User/Controller/Adminhtml/User/Role/Delete.php

100644100755
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@ public function execute()
2525
return;
2626
}
2727

28-
try {
29-
$this->_initRole()->delete();
30-
$this->messageManager->addSuccess(__('You deleted the role.'));
31-
} catch (\Exception $e) {
32-
$this->messageManager->addError(__('An error occurred while deleting this role.'));
33-
}
34-
28+
$this->_initRole()->delete();
29+
$this->messageManager->addSuccess(__('You deleted the role.'));
3530
$this->_redirect("*/*/");
3631
}
3732
}

app/code/Magento/User/Controller/Adminhtml/User/Role/SaveRole.php

100644100755
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,27 @@ public function execute()
7878
return;
7979
}
8080

81-
try {
82-
$roleName = $this->_filterManager->removeTags($this->getRequest()->getParam('rolename', false));
83-
84-
$role->setName($roleName)
85-
->setPid($this->getRequest()->getParam('parent_id', false))
86-
->setRoleType(RoleGroup::ROLE_TYPE)
87-
->setUserType(UserContextInterface::USER_TYPE_ADMIN);
88-
$this->_eventManager->dispatch(
89-
'admin_permissions_role_prepare_save',
90-
['object' => $role, 'request' => $this->getRequest()]
91-
);
92-
$role->save();
81+
$roleName = $this->_filterManager->removeTags($this->getRequest()->getParam('rolename', false));
82+
$role->setName($roleName)
83+
->setPid($this->getRequest()->getParam('parent_id', false))
84+
->setRoleType(RoleGroup::ROLE_TYPE)
85+
->setUserType(UserContextInterface::USER_TYPE_ADMIN);
86+
$this->_eventManager->dispatch(
87+
'admin_permissions_role_prepare_save',
88+
['object' => $role, 'request' => $this->getRequest()]
89+
);
90+
$role->save();
9391

94-
$this->_rulesFactory->create()->setRoleId($role->getId())->setResources($resource)->saveRel();
92+
$this->_rulesFactory->create()->setRoleId($role->getId())->setResources($resource)->saveRel();
9593

96-
foreach ($oldRoleUsers as $oUid) {
97-
$this->_deleteUserFromRole($oUid, $role->getId());
98-
}
94+
foreach ($oldRoleUsers as $oUid) {
95+
$this->_deleteUserFromRole($oUid, $role->getId());
96+
}
9997

100-
foreach ($roleUsers as $nRuid) {
101-
$this->_addUserToRole($nRuid, $role->getId());
102-
}
103-
$this->messageManager->addSuccess(__('You saved the role.'));
104-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
105-
$this->messageManager->addError($e->getMessage());
106-
} catch (\Exception $e) {
107-
$this->messageManager->addError(__('An error occurred while saving this role.'));
98+
foreach ($roleUsers as $nRuid) {
99+
$this->_addUserToRole($nRuid, $role->getId());
108100
}
101+
$this->messageManager->addSuccess(__('You saved the role.'));
109102
$this->_redirect('adminhtml/*/');
110103
return;
111104
}

app/code/Magento/Wishlist/Controller/Index/Add.php

100644100755
Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,45 +91,33 @@ public function execute()
9191
return;
9292
}
9393

94-
try {
95-
$buyRequest = new \Magento\Framework\Object($requestParams);
96-
97-
$result = $wishlist->addNewItem($product, $buyRequest);
98-
if (is_string($result)) {
99-
throw new \Magento\Framework\Exception\LocalizedException(__($result));
100-
}
101-
$wishlist->save();
102-
103-
$this->_eventManager->dispatch(
104-
'wishlist_add_product',
105-
['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
106-
);
107-
108-
$referer = $session->getBeforeWishlistUrl();
109-
if ($referer) {
110-
$session->setBeforeWishlistUrl(null);
111-
} else {
112-
$referer = $this->_redirect->getRefererUrl();
113-
}
114-
115-
116-
/** @var $helper \Magento\Wishlist\Helper\Data */
117-
$helper = $this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate();
118-
$message = __(
119-
'%1 has been added to your wishlist. Click <a href="%2">here</a> to continue shopping.',
120-
$this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($product->getName()),
121-
$this->_objectManager->get('Magento\Framework\Escaper')->escapeUrl($referer)
122-
);
123-
$this->messageManager->addSuccess($message);
124-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
125-
$this->messageManager->addError(
126-
__('An error occurred while adding item to wish list: %1', $e->getMessage())
127-
);
128-
} catch (\Exception $e) {
129-
$this->messageManager->addError(__('An error occurred while adding item to wish list.'));
130-
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
94+
$buyRequest = new \Magento\Framework\Object($requestParams);
95+
96+
$result = $wishlist->addNewItem($product, $buyRequest);
97+
if (is_string($result)) {
98+
throw new \Magento\Framework\Exception\LocalizedException(__($result));
99+
}
100+
$wishlist->save();
101+
102+
$this->_eventManager->dispatch(
103+
'wishlist_add_product',
104+
['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
105+
);
106+
107+
$referer = $session->getBeforeWishlistUrl();
108+
if ($referer) {
109+
$session->setBeforeWishlistUrl(null);
110+
} else {
111+
$referer = $this->_redirect->getRefererUrl();
131112
}
132113

114+
$this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate();
115+
$message = __(
116+
'%1 has been added to your wishlist. Click <a href="%2">here</a> to continue shopping.',
117+
$this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($product->getName()),
118+
$this->_objectManager->get('Magento\Framework\Escaper')->escapeUrl($referer)
119+
);
120+
$this->messageManager->addSuccess($message);
133121
$this->_redirect('*', ['wishlist_id' => $wishlist->getId()]);
134122
}
135123
}

app/code/Magento/Wishlist/Controller/Index/Fromcart.php

100644100755
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,30 @@ public function execute()
4646

4747
/* @var \Magento\Checkout\Model\Cart $cart */
4848
$cart = $this->_objectManager->get('Magento\Checkout\Model\Cart');
49-
$session = $this->_objectManager->get('Magento\Checkout\Model\Session');
49+
$this->_objectManager->get('Magento\Checkout\Model\Session');
5050

51-
try {
52-
$item = $cart->getQuote()->getItemById($itemId);
53-
if (!$item) {
54-
throw new \Magento\Framework\Exception\LocalizedException(
55-
__('The requested cart item doesn\'t exist.')
56-
);
57-
}
51+
$item = $cart->getQuote()->getItemById($itemId);
52+
if (!$item) {
53+
throw new \Magento\Framework\Exception\LocalizedException(
54+
__('The requested cart item doesn\'t exist.')
55+
);
56+
}
5857

59-
$productId = $item->getProductId();
60-
$buyRequest = $item->getBuyRequest();
58+
$productId = $item->getProductId();
59+
$buyRequest = $item->getBuyRequest();
6160

62-
$wishlist->addNewItem($productId, $buyRequest);
61+
$wishlist->addNewItem($productId, $buyRequest);
6362

64-
$productIds[] = $productId;
65-
$cart->getQuote()->removeItem($itemId);
66-
$cart->save();
67-
$this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate();
68-
$productName = $this->_objectManager->get('Magento\Framework\Escaper')
69-
->escapeHtml($item->getProduct()->getName());
70-
$wishlistName = $this->_objectManager->get('Magento\Framework\Escaper')
71-
->escapeHtml($wishlist->getName());
72-
$this->messageManager->addSuccess(__("%1 has been moved to wish list %2", $productName, $wishlistName));
73-
$wishlist->save();
74-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
75-
$this->messageManager->addError($e->getMessage());
76-
} catch (\Exception $e) {
77-
$this->messageManager->addException($e, __('We can\'t move the item to the wish list.'));
78-
}
63+
$productIds[] = $productId;
64+
$cart->getQuote()->removeItem($itemId);
65+
$cart->save();
66+
$this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate();
67+
$productName = $this->_objectManager->get('Magento\Framework\Escaper')
68+
->escapeHtml($item->getProduct()->getName());
69+
$wishlistName = $this->_objectManager->get('Magento\Framework\Escaper')
70+
->escapeHtml($wishlist->getName());
71+
$this->messageManager->addSuccess(__("%1 has been moved to wish list %2", $productName, $wishlistName));
72+
$wishlist->save();
7973

8074
return $this->getResponse()->setRedirect(
8175
$this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl()

app/code/Magento/Wishlist/Test/Unit/Controller/Index/AddTest.php

100644100755
Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ public function testExecuteWithProductIdAndWithoutProduct()
434434

435435
/**
436436
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
437+
* @expectedException \Magento\Framework\Exception\LocalizedException
437438
*/
438439
public function testExecuteWithProductAndCantAddProductToWishlist()
439440
{
@@ -443,17 +444,11 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
443444
->method('addNewItem')
444445
->will($this->returnValue('Can\'t add product to wishlist'));
445446

446-
$wishlist
447-
->expects($this->once())
448-
->method('getId')
449-
->will($this->returnValue(2));
450-
451447
$this->wishlistProvider
452448
->expects($this->once())
453449
->method('getWishlist')
454450
->will($this->returnValue($wishlist));
455451

456-
457452
$request = $this->getMock('Magento\Framework\App\Request\Http', ['getParams'], [], '', false);
458453
$request
459454
->expects($this->once())
@@ -465,20 +460,8 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
465460
$eventManager = $this->getMock('Magento\Framework\Event\Manager', null, [], '', false);
466461
$url = $this->getMock('Magento\Framework\Url', null, [], '', false);
467462
$actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', null, [], '', false);
468-
$redirect = $this->getMock('\Magento\Store\App\Response\Redirect', ['redirect'], [], '', false);
469-
$redirect
470-
->expects($this->once())
471-
->method('redirect')
472-
->with($response, '*', ['wishlist_id' => 2])
473-
->will($this->returnValue(null));
474463

475464
$view = $this->getMock('Magento\Framework\App\View', null, [], '', false);
476-
$messageManager = $this->getMock('Magento\Framework\Message\Manager', ['addError'], [], '', false);
477-
$messageManager
478-
->expects($this->once())
479-
->method('addError')
480-
->with('An error occurred while adding item to wish list: Can\'t add product to wishlist')
481-
->will($this->returnValue(null));
482465

483466
$this->context
484467
->expects($this->any())
@@ -504,18 +487,10 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
504487
->expects($this->any())
505488
->method('getActionFlag')
506489
->will($this->returnValue($actionFlag));
507-
$this->context
508-
->expects($this->any())
509-
->method('getRedirect')
510-
->will($this->returnValue($redirect));
511490
$this->context
512491
->expects($this->any())
513492
->method('getView')
514493
->will($this->returnValue($view));
515-
$this->context
516-
->expects($this->any())
517-
->method('getMessageManager')
518-
->will($this->returnValue($messageManager));
519494

520495
$this->customerSession
521496
->expects($this->exactly(1))
@@ -637,19 +612,6 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
637612
->with('http://test-url.com')
638613
->will($this->returnValue('http://test-url.com'));
639614

640-
$logger = $this->getMock(
641-
'Magento\Framework\Logger\Monolog',
642-
['critical'],
643-
[],
644-
'',
645-
false
646-
);
647-
$logger
648-
->expects($this->once())
649-
->method('critical')
650-
->with($exception)
651-
->will($this->returnValue(true));
652-
653615
$om = $this->getMock('Magento\Framework\App\ObjectManager', ['get'], [], '', false);
654616
$om
655617
->expects($this->at(0))
@@ -666,11 +628,6 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
666628
->method('get')
667629
->with('Magento\Framework\Escaper')
668630
->will($this->returnValue($escaper));
669-
$om
670-
->expects($this->at(3))
671-
->method('get')
672-
->with('Psr\Log\LoggerInterface')
673-
->will($this->returnValue($logger));
674631

675632
$response = $this->getMock('Magento\Framework\App\Response\Http', null, [], '', false);
676633
$eventManager = $this->getMock('Magento\Framework\Event\Manager', ['dispatch'], [], '', false);
@@ -700,13 +657,7 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
700657
);
701658
$messageManager
702659
->expects($this->once())
703-
->method('addError')
704-
->with('An error occurred while adding item to wish list.')
705-
->will($this->returnValue(null));
706-
$messageManager
707-
->expects($this->once())
708-
->method('addSuccess')
709-
->will($this->throwException($exception));
660+
->method('addSuccess');
710661

711662
$this->context
712663
->expects($this->any())

0 commit comments

Comments
 (0)