Skip to content

Commit 00953bc

Browse files
author
Jeroen
committed
Refactor \Order\Shipment\Save Controller to use ResultInterface
1 parent 3ff0161 commit 00953bc

File tree

2 files changed

+30
-51
lines changed
  • app/code/Magento/Shipping
    • Controller/Adminhtml/Order/Shipment
    • Test/Unit/Controller/Adminhtml/Order/Shipment

2 files changed

+30
-51
lines changed

app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
77
namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
88

9-
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
10-
use Magento\Backend\App\Action;
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Framework\Controller\ResultFactory;
1111
use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator;
1212

1313
/**
@@ -48,17 +48,22 @@ class Save extends \Magento\Backend\App\Action implements HttpPostActionInterfac
4848
* @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
4949
* @param \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator
5050
* @param \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender
51+
* @param \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface|null $shipmentValidator
5152
*/
5253
public function __construct(
5354
\Magento\Backend\App\Action\Context $context,
5455
\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader,
5556
\Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator,
56-
\Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender
57+
\Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender,
58+
\Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface $shipmentValidator = null
5759
) {
60+
parent::__construct($context);
61+
5862
$this->shipmentLoader = $shipmentLoader;
5963
$this->labelGenerator = $labelGenerator;
6064
$this->shipmentSender = $shipmentSender;
61-
parent::__construct($context);
65+
$this->shipmentValidator = $shipmentValidator ?: \Magento\Framework\App\ObjectManager::getInstance()
66+
->get(\Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class);
6267
}
6368

6469
/**
@@ -86,7 +91,7 @@ protected function _saveShipment($shipment)
8691
* Save shipment
8792
* We can save only new shipment. Existing shipments are not editable
8893
*
89-
* @return void
94+
* @return \Magento\Backend\Model\View\Result\Redirect
9095
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
9196
* @SuppressWarnings(PHPMD.NPathComplexity)
9297
*/
@@ -98,7 +103,7 @@ public function execute()
98103
$formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
99104
$isPost = $this->getRequest()->isPost();
100105
if (!$formKeyIsValid || !$isPost) {
101-
$this->messageManager->addError(__('We can\'t save the shipment right now.'));
106+
$this->messageManager->addErrorMessage(__('We can\'t save the shipment right now.'));
102107
return $resultRedirect->setPath('sales/order/index');
103108
}
104109

@@ -118,8 +123,7 @@ public function execute()
118123
$this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
119124
$shipment = $this->shipmentLoader->load();
120125
if (!$shipment) {
121-
$this->_forward('noroute');
122-
return;
126+
return $this->resultFactory->create(ResultFactory::TYPE_FORWARD)->forward('noroute');
123127
}
124128

125129
if (!empty($data['comment_text'])) {
@@ -132,15 +136,13 @@ public function execute()
132136
$shipment->setCustomerNote($data['comment_text']);
133137
$shipment->setCustomerNoteNotify(isset($data['comment_customer_notify']));
134138
}
135-
$validationResult = $this->getShipmentValidator()
136-
->validate($shipment, [QuantityValidator::class]);
139+
$validationResult = $this->shipmentValidator->validate($shipment, [QuantityValidator::class]);
137140

138141
if ($validationResult->hasMessages()) {
139-
$this->messageManager->addError(
142+
$this->messageManager->addErrorMessage(
140143
__("Shipment Document Validation Error(s):\n" . implode("\n", $validationResult->getMessages()))
141144
);
142-
$this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
143-
return;
145+
return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
144146
}
145147
$shipment->register();
146148

@@ -160,7 +162,7 @@ public function execute()
160162
$shipmentCreatedMessage = __('The shipment has been created.');
161163
$labelCreatedMessage = __('You created the shipping label.');
162164

163-
$this->messageManager->addSuccess(
165+
$this->messageManager->addSuccessMessage(
164166
$isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage : $shipmentCreatedMessage
165167
);
166168
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->getCommentText(true);
@@ -169,38 +171,23 @@ public function execute()
169171
$responseAjax->setError(true);
170172
$responseAjax->setMessage($e->getMessage());
171173
} else {
172-
$this->messageManager->addError($e->getMessage());
173-
$this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
174+
$this->messageManager->addErrorMessage($e->getMessage());
175+
return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
174176
}
175177
} catch (\Exception $e) {
176178
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
177179
if ($isNeedCreateLabel) {
178180
$responseAjax->setError(true);
179181
$responseAjax->setMessage(__('An error occurred while creating shipping label.'));
180182
} else {
181-
$this->messageManager->addError(__('Cannot save shipment.'));
182-
$this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
183+
$this->messageManager->addErrorMessage(__('Cannot save shipment.'));
184+
return $resultRedirect->setPath('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]);
183185
}
184186
}
185187
if ($isNeedCreateLabel) {
186-
$this->getResponse()->representJson($responseAjax->toJson());
187-
} else {
188-
$this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]);
189-
}
190-
}
191-
192-
/**
193-
* @return \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface
194-
* @deprecated 100.1.1
195-
*/
196-
private function getShipmentValidator()
197-
{
198-
if ($this->shipmentValidator === null) {
199-
$this->shipmentValidator = $this->_objectManager->get(
200-
\Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class
201-
);
188+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setJsonData($responseAjax->toJson());
202189
}
203190

204-
return $this->shipmentValidator;
191+
return $resultRedirect->setPath('sales/order/view', ['order_id' => $shipment->getOrderId()]);
205192
}
206193
}

app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function setUp()
142142
);
143143
$this->messageManager = $this->createPartialMock(
144144
\Magento\Framework\Message\Manager::class,
145-
['addSuccess', 'addError']
145+
['addSuccessMessage', 'addErrorMessage']
146146
);
147147
$this->session = $this->createPartialMock(
148148
\Magento\Backend\Model\Session::class,
@@ -236,7 +236,7 @@ public function testExecute($formKeyIsValid, $isPost)
236236

237237
if (!$formKeyIsValid || !$isPost) {
238238
$this->messageManager->expects($this->once())
239-
->method('addError');
239+
->method('addErrorMessage');
240240

241241
$this->resultRedirect->expects($this->once())
242242
->method('setPath')
@@ -325,12 +325,11 @@ public function testExecute($formKeyIsValid, $isPost)
325325
->method('get')
326326
->with(\Magento\Backend\Model\Session::class)
327327
->will($this->returnValue($this->session));
328-
$path = 'sales/order/view';
329328
$arguments = ['order_id' => $orderId];
330329
$shipment->expects($this->once())
331330
->method('getOrderId')
332331
->will($this->returnValue($orderId));
333-
$this->prepareRedirect($path, $arguments);
332+
$this->prepareRedirect($arguments);
334333

335334
$this->shipmentValidatorMock->expects($this->once())
336335
->method('validate')
@@ -360,10 +359,9 @@ public function executeDataProvider()
360359
}
361360

362361
/**
363-
* @param string $path
364362
* @param array $arguments
365363
*/
366-
protected function prepareRedirect($path, array $arguments = [])
364+
protected function prepareRedirect(array $arguments = [])
367365
{
368366
$this->actionFlag->expects($this->any())
369367
->method('get')
@@ -372,14 +370,8 @@ protected function prepareRedirect($path, array $arguments = [])
372370
$this->session->expects($this->any())
373371
->method('setIsUrlNotice')
374372
->with(true);
375-
376-
$url = $path . '/' . (!empty($arguments) ? $arguments['order_id'] : '');
377-
$this->helper->expects($this->atLeastOnce())
378-
->method('getUrl')
379-
->with($path, $arguments)
380-
->will($this->returnValue($url));
381-
$this->response->expects($this->atLeastOnce())
382-
->method('setRedirect')
383-
->with($url);
373+
$this->resultRedirect->expects($this->once())
374+
->method('setPath')
375+
->with('sales/order/view', $arguments);
384376
}
385377
}

0 commit comments

Comments
 (0)