1
1
<?php
2
2
/**
3
- *
4
3
* Copyright © Magento, Inc. All rights reserved.
5
4
* See COPYING.txt for license details.
6
5
*/
6
+ declare (strict_types=1 );
7
+
7
8
namespace Magento \Shipping \Controller \Adminhtml \Order \Shipment ;
8
9
9
10
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 ;
10
20
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
12
25
{
13
26
/**
14
27
* Authorization level of a basic admin session
@@ -18,56 +31,84 @@ class AddTrack extends \Magento\Backend\App\Action
18
31
const ADMIN_RESOURCE = 'Magento_Sales::shipment ' ;
19
32
20
33
/**
21
- * @var \Magento\Shipping\Controller\Adminhtml\Order\ ShipmentLoader
34
+ * @var ShipmentLoader
22
35
*/
23
36
protected $ shipmentLoader ;
24
37
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
+
25
53
/**
26
54
* @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
28
59
*/
29
60
public function __construct (
30
61
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
32
66
) {
33
- $ this ->shipmentLoader = $ shipmentLoader ;
34
67
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);
35
76
}
36
77
37
78
/**
38
- * Add new tracking number action
79
+ * Add new tracking number action.
39
80
*
40
- * @return void
41
- * @throws \Magento\Framework\Exception\LocalizedException
81
+ * @return ResultInterface
42
82
*/
43
83
public function execute ()
44
84
{
45
85
try {
46
86
$ carrier = $ this ->getRequest ()->getPost ('carrier ' );
47
87
$ number = $ this ->getRequest ()->getPost ('number ' );
48
88
$ title = $ this ->getRequest ()->getPost ('title ' );
89
+
49
90
if (empty ($ carrier )) {
50
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Please specify a carrier. ' ));
91
+ throw new LocalizedException (__ ('Please specify a carrier. ' ));
51
92
}
52
93
if (empty ($ number )) {
53
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Please enter a tracking number. ' ));
94
+ throw new LocalizedException (__ ('Please enter a tracking number. ' ));
54
95
}
96
+
55
97
$ this ->shipmentLoader ->setOrderId ($ this ->getRequest ()->getParam ('order_id ' ));
56
98
$ this ->shipmentLoader ->setShipmentId ($ this ->getRequest ()->getParam ('shipment_id ' ));
57
99
$ this ->shipmentLoader ->setShipment ($ this ->getRequest ()->getParam ('shipment ' ));
58
100
$ this ->shipmentLoader ->setTracking ($ this ->getRequest ()->getParam ('tracking ' ));
59
101
$ shipment = $ this ->shipmentLoader ->load ();
60
102
if ($ shipment ) {
61
- $ track = $ this ->_objectManager ->create (
62
- \Magento \Sales \Model \Order \Shipment \Track::class
63
- )->setNumber (
103
+ $ track = $ this ->trackFactory ->create ()->setNumber (
64
104
$ number
65
105
)->setCarrierCode (
66
106
$ carrier
67
107
)->setTitle (
68
108
$ title
69
109
);
70
- $ shipment ->addTrack ($ track )->save ();
110
+ $ shipment ->addTrack ($ track );
111
+ $ this ->shipmentRepository ->save ($ shipment );
71
112
72
113
$ this ->_view ->loadLayout ();
73
114
$ this ->_view ->getPage ()->getConfig ()->getTitle ()->prepend (__ ('Shipments ' ));
@@ -78,16 +119,18 @@ public function execute()
78
119
'message ' => __ ('We can \'t initialize shipment for adding tracking number. ' ),
79
120
];
80
121
}
81
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
122
+ } catch (LocalizedException $ e ) {
82
123
$ response = ['error ' => true , 'message ' => $ e ->getMessage ()];
83
124
} catch (\Exception $ e ) {
84
125
$ response = ['error ' => true , 'message ' => __ ('Cannot add tracking number. ' )];
85
126
}
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 );
91
132
}
133
+
134
+ return $ this ->resultFactory ->create (ResultFactory::TYPE_RAW )->setContents ($ response );
92
135
}
93
136
}
0 commit comments