Skip to content

Commit 65634f6

Browse files
author
Jeroen
committed
Refactor \Order\Shipment\AddTrack Controller to use ResultInterface
1 parent d1ebc17 commit 65634f6

File tree

1 file changed

+62
-21
lines changed
  • app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment

1 file changed

+62
-21
lines changed
Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
89

910
use Magento\Backend\App\Action;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Controller\ResultFactory;
13+
use Magento\Framework\Controller\ResultInterface;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\Framework\Serialize\SerializerInterface;
17+
use Magento\Sales\Api\Data\ShipmentTrackInterfaceFactory;
18+
use Magento\Sales\Api\ShipmentRepositoryInterface;
19+
use Magento\Sales\Model\Order\Shipment\TrackFactory;
20+
use Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader;
1021

1122
class AddTrack extends \Magento\Backend\App\Action
1223
{
@@ -18,56 +29,84 @@ class AddTrack extends \Magento\Backend\App\Action
1829
const ADMIN_RESOURCE = 'Magento_Sales::shipment';
1930

2031
/**
21-
* @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader
32+
* @var ShipmentLoader
2233
*/
2334
protected $shipmentLoader;
2435

36+
/**
37+
* @var ShipmentRepositoryInterface
38+
*/
39+
private $shipmentRepository;
40+
41+
/**
42+
* @var TrackFactory
43+
*/
44+
private $trackFactory;
45+
46+
/**
47+
* @var SerializerInterface
48+
*/
49+
private $serializer;
50+
2551
/**
2652
* @param Action\Context $context
27-
* @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
53+
* @param ShipmentLoader $shipmentLoader
54+
* @param ShipmentRepositoryInterface|null $shipmentRepository
55+
* @param TrackFactory|null $trackFactory
56+
* @param SerializerInterface|null $serializer
2857
*/
2958
public function __construct(
3059
Action\Context $context,
31-
\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
60+
ShipmentLoader $shipmentLoader,
61+
ShipmentRepositoryInterface $shipmentRepository = null,
62+
TrackFactory $trackFactory = null,
63+
SerializerInterface $serializer = null
3264
) {
33-
$this->shipmentLoader = $shipmentLoader;
3465
parent::__construct($context);
66+
67+
$this->shipmentLoader = $shipmentLoader;
68+
$this->shipmentRepository = $shipmentRepository ?: ObjectManager::getInstance()
69+
->get(ShipmentRepositoryInterface::class);
70+
$this->trackFactory = $trackFactory ?: ObjectManager::getInstance()
71+
->get(ShipmentTrackInterfaceFactory::class);
72+
$this->serializer = $serializer ?: ObjectManager::getInstance()
73+
->get(SerializerInterface::class);
3574
}
3675

3776
/**
38-
* Add new tracking number action
77+
* Add new tracking number action.
3978
*
40-
* @return void
41-
* @throws \Magento\Framework\Exception\LocalizedException
79+
* @return ResultInterface
4280
*/
43-
public function execute()
81+
public function execute(): ResultInterface
4482
{
4583
try {
4684
$carrier = $this->getRequest()->getPost('carrier');
4785
$number = $this->getRequest()->getPost('number');
4886
$title = $this->getRequest()->getPost('title');
87+
4988
if (empty($carrier)) {
50-
throw new \Magento\Framework\Exception\LocalizedException(__('Please specify a carrier.'));
89+
throw new LocalizedException(__('Please specify a carrier.'));
5190
}
5291
if (empty($number)) {
53-
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a tracking number.'));
92+
throw new LocalizedException(__('Please enter a tracking number.'));
5493
}
94+
5595
$this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
5696
$this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
5797
$this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
5898
$this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
5999
$shipment = $this->shipmentLoader->load();
60100
if ($shipment) {
61-
$track = $this->_objectManager->create(
62-
\Magento\Sales\Model\Order\Shipment\Track::class
63-
)->setNumber(
101+
$track = $this->trackFactory->create()->setNumber(
64102
$number
65103
)->setCarrierCode(
66104
$carrier
67105
)->setTitle(
68106
$title
69107
);
70-
$shipment->addTrack($track)->save();
108+
$shipment->addTrack($track);
109+
$this->shipmentRepository->save($shipment);
71110

72111
$this->_view->loadLayout();
73112
$this->_view->getPage()->getConfig()->getTitle()->prepend(__('Shipments'));
@@ -78,16 +117,18 @@ public function execute()
78117
'message' => __('We can\'t initialize shipment for adding tracking number.'),
79118
];
80119
}
81-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
120+
} catch (LocalizedException $e) {
82121
$response = ['error' => true, 'message' => $e->getMessage()];
83122
} catch (\Exception $e) {
84123
$response = ['error' => true, 'message' => __('Cannot add tracking number.')];
85124
}
86-
if (is_array($response)) {
87-
$response = $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($response);
88-
$this->getResponse()->representJson($response);
89-
} else {
90-
$this->getResponse()->setBody($response);
125+
126+
if (\is_array($response)) {
127+
$response = $this->serializer->serialize($response);
128+
129+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setJsonData($response);
91130
}
131+
132+
return $this->resultFactory->create(ResultFactory::TYPE_RAW)->setContents($response);
92133
}
93134
}

0 commit comments

Comments
 (0)