Skip to content

Commit bcf3358

Browse files
authored
ENGCOM-4532: Refactor \Order\Shipment\AddTrack Controller to use ResultInterface #21822
2 parents 33ddb30 + 0420980 commit bcf3358

File tree

2 files changed

+155
-75
lines changed

2 files changed

+155
-75
lines changed
Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
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\Action\HttpPostActionInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\Controller\ResultInterface;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Serialize\SerializerInterface;
17+
use Magento\Sales\Api\Data\ShipmentTrackInterfaceFactory;
18+
use Magento\Sales\Api\ShipmentRepositoryInterface;
19+
use Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader;
1020

11-
class AddTrack extends \Magento\Backend\App\Action
21+
/**
22+
* Add new tracking number to shipment controller.
23+
*/
24+
class AddTrack extends Action implements HttpPostActionInterface
1225
{
1326
/**
1427
* Authorization level of a basic admin session
@@ -18,56 +31,84 @@ class AddTrack extends \Magento\Backend\App\Action
1831
const ADMIN_RESOURCE = 'Magento_Sales::shipment';
1932

2033
/**
21-
* @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader
34+
* @var ShipmentLoader
2235
*/
2336
protected $shipmentLoader;
2437

38+
/**
39+
* @var ShipmentRepositoryInterface
40+
*/
41+
private $shipmentRepository;
42+
43+
/**
44+
* @var ShipmentTrackInterfaceFactory
45+
*/
46+
private $trackFactory;
47+
48+
/**
49+
* @var SerializerInterface
50+
*/
51+
private $serializer;
52+
2553
/**
2654
* @param Action\Context $context
27-
* @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
55+
* @param ShipmentLoader $shipmentLoader
56+
* @param ShipmentRepositoryInterface|null $shipmentRepository
57+
* @param ShipmentTrackInterfaceFactory|null $trackFactory
58+
* @param SerializerInterface|null $serializer
2859
*/
2960
public function __construct(
3061
Action\Context $context,
31-
\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
62+
ShipmentLoader $shipmentLoader,
63+
ShipmentRepositoryInterface $shipmentRepository = null,
64+
ShipmentTrackInterfaceFactory $trackFactory = null,
65+
SerializerInterface $serializer = null
3266
) {
33-
$this->shipmentLoader = $shipmentLoader;
3467
parent::__construct($context);
68+
69+
$this->shipmentLoader = $shipmentLoader;
70+
$this->shipmentRepository = $shipmentRepository ?: ObjectManager::getInstance()
71+
->get(ShipmentRepositoryInterface::class);
72+
$this->trackFactory = $trackFactory ?: ObjectManager::getInstance()
73+
->get(ShipmentTrackInterfaceFactory::class);
74+
$this->serializer = $serializer ?: ObjectManager::getInstance()
75+
->get(SerializerInterface::class);
3576
}
3677

3778
/**
38-
* Add new tracking number action
79+
* Add new tracking number action.
3980
*
40-
* @return void
41-
* @throws \Magento\Framework\Exception\LocalizedException
81+
* @return ResultInterface
4282
*/
4383
public function execute()
4484
{
4585
try {
4686
$carrier = $this->getRequest()->getPost('carrier');
4787
$number = $this->getRequest()->getPost('number');
4888
$title = $this->getRequest()->getPost('title');
89+
4990
if (empty($carrier)) {
50-
throw new \Magento\Framework\Exception\LocalizedException(__('Please specify a carrier.'));
91+
throw new LocalizedException(__('Please specify a carrier.'));
5192
}
5293
if (empty($number)) {
53-
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a tracking number.'));
94+
throw new LocalizedException(__('Please enter a tracking number.'));
5495
}
96+
5597
$this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
5698
$this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
5799
$this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
58100
$this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
59101
$shipment = $this->shipmentLoader->load();
60102
if ($shipment) {
61-
$track = $this->_objectManager->create(
62-
\Magento\Sales\Model\Order\Shipment\Track::class
63-
)->setNumber(
103+
$track = $this->trackFactory->create()->setNumber(
64104
$number
65105
)->setCarrierCode(
66106
$carrier
67107
)->setTitle(
68108
$title
69109
);
70-
$shipment->addTrack($track)->save();
110+
$shipment->addTrack($track);
111+
$this->shipmentRepository->save($shipment);
71112

72113
$this->_view->loadLayout();
73114
$this->_view->getPage()->getConfig()->getTitle()->prepend(__('Shipments'));
@@ -78,16 +119,18 @@ public function execute()
78119
'message' => __('We can\'t initialize shipment for adding tracking number.'),
79120
];
80121
}
81-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
122+
} catch (LocalizedException $e) {
82123
$response = ['error' => true, 'message' => $e->getMessage()];
83124
} catch (\Exception $e) {
84125
$response = ['error' => true, 'message' => __('Cannot add tracking number.')];
85126
}
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);
127+
128+
if (\is_array($response)) {
129+
$response = $this->serializer->serialize($response);
130+
131+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setJsonData($response);
91132
}
133+
134+
return $this->resultFactory->create(ResultFactory::TYPE_RAW)->setContents($response);
92135
}
93136
}

0 commit comments

Comments
 (0)