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