1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\Rest\EntityMapper;
use Magento\Sales\Api\Data\OrderAddressInterfaceFactory;
use Magento\Sales\Model\Order\Address\Renderer;
use Temando\Shipping\Model\BatchInterface;
use Temando\Shipping\Model\BatchInterfaceFactory;
use Temando\Shipping\Model\Shipment\ShipmentErrorInterface;
use Temando\Shipping\Model\Shipment\ShipmentErrorInterfaceFactory;
use Temando\Shipping\Model\Shipment\ShipmentSummaryInterface;
use Temando\Shipping\Model\Shipment\ShipmentSummaryInterfaceFactory;
use Temando\Shipping\Rest\Response\DataObject\Batch;
use Temando\Shipping\Rest\Response\DataObject\Shipment;
use Temando\Shipping\Rest\Response\Fields\Batch\Shipment as ShipmentReference;
use Temando\Shipping\Rest\Response\Fields\LocationAttributes;
/**
* Map API data to application data object
*
* @package Temando\Shipping\Rest
* @author Rhodri Davies <rhodri.davies@temando.com>
* @author Benjamin Heuer <benjamin.heuer@netresearch.de>
* @author Sebastian Ertner <sebastian.ertner@netresearch.de>
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.temando.com/
*/
class BatchResponseMapper
{
/**
* @var BatchInterfaceFactory
*
*/
private $batchFactory;
/**
* @var ShipmentSummaryInterfaceFactory
*/
private $shipmentFactory;
/**
* @var ShipmentErrorInterfaceFactory
*/
private $errorFactory;
/**
* @var OrderAddressInterfaceFactory
*/
private $addressFactory;
/**
* @var Renderer
*/
private $addressRenderer;
/**
* DispatchResponseMapper constructor.
*
* @param BatchInterfaceFactory $batchFactory
* @param ShipmentSummaryInterfaceFactory $shipmentFactory
* @param ShipmentErrorInterfaceFactory $errorFactory
* @param OrderAddressInterfaceFactory $addressFactory
* @param Renderer $addressRenderer
*/
public function __construct(
BatchInterfaceFactory $batchFactory,
ShipmentSummaryInterfaceFactory $shipmentFactory,
ShipmentErrorInterfaceFactory $errorFactory,
OrderAddressInterfaceFactory $addressFactory,
Renderer $addressRenderer
) {
$this->batchFactory = $batchFactory;
$this->shipmentFactory = $shipmentFactory;
$this->errorFactory = $errorFactory;
$this->addressFactory = $addressFactory;
$this->addressRenderer = $addressRenderer;
}
/**
* @param LocationAttributes $location
*
* @return string
*/
private function getFormattedDestinationAddress(LocationAttributes $location)
{
$addressData = [
'region' => $location->getAddress()->getAdministrativeArea(),
'postcode' => $location->getAddress()->getPostalCode(),
'street' => implode(' ', $location->getAddress()->getLines()),
'city' => $location->getAddress()->getLocality(),
'country_id' => $location->getAddress()->getCountryCode(),
'company' => $location->getContact()->getOrganisationName()
];
/** @var \Magento\Sales\Api\Data\OrderAddressInterface|\Magento\Sales\Model\Order\Address $address */
$address = $this->addressFactory->create(['data' => $addressData]);
$formattedAddress = $this->addressRenderer->format($address, 'html');
return (string)$formattedAddress;
}
/**
* @param ShipmentReference $apiShipmentReference
* @param Shipment $apiShipment
*
* @return ShipmentSummaryInterface
*/
private function mapShipment(ShipmentReference $apiShipmentReference, Shipment $apiShipment)
{
$errors = [];
foreach ($apiShipmentReference->getErrors() as $apiError) {
$errors[]= $this->errorFactory->create(['data' => [
ShipmentErrorInterface::TITLE => $apiError->getTitle(),
ShipmentErrorInterface::DETAIL => $apiError->getDetail(),
]]);
}
$destination = $apiShipment->getAttributes()->getDestination();
// fixme(nr): this is wrong! mappers do not prepare for display. pass on full location instead
$address = $this->getFormattedDestinationAddress($destination);
$recipientName = [
$destination->getContact()->getPersonFirstName(),
$destination->getContact()->getPersonLastName(),
];
// fixme(nr): fatal error, shipment.order is not a required field
$shipment = $this->shipmentFactory->create(['data' => [
ShipmentSummaryInterface::ORDER_ID => $apiShipment->getAttributes()->getOrder()->getReference(),
ShipmentSummaryInterface::SHIPMENT_ID => $apiShipment->getId(),
ShipmentSummaryInterface::STATUS => $apiShipment->getAttributes()->getStatus(),
ShipmentSummaryInterface::ERRORS => $errors,
ShipmentSummaryInterface::RECIPIENT_ADDRESS => $address,
ShipmentSummaryInterface::RECIPIENT_NAME => implode(' ', $recipientName),
]]);
return $shipment;
}
/**
* @param Batch $apiBatch
*
* @return BatchInterface
*/
public function map(Batch $apiBatch)
{
$batchId = $apiBatch->getId();
$status = $apiBatch->getAttributes()->getStatus();
$createdAtDate = $apiBatch->getAttributes()->getCreatedAt();
$updatedAtDate = $apiBatch->getAttributes()->getModifiedAt();
$failedShipments = [];
$includedShipments = [];
$documentation = $apiBatch->getAttributes()->getDocumentation();
$shipments = [];
foreach ($apiBatch->getShipments() as $shipment) {
$shipments[$shipment->getId()] = $shipment;
}
// split shipments into failed and successfully created
foreach ($apiBatch->getAttributes()->getShipments() as $shipmentReference) {
$mappedShipment = $this->mapShipment($shipmentReference, $shipments[$shipmentReference->getId()]);
if ($shipmentReference->getStatus() === 'error') {
$failedShipments[$shipmentReference->getId()] = $mappedShipment;
} else {
$includedShipments[$shipmentReference->getId()] = $mappedShipment;
}
}
$batch = $this->batchFactory->create(['data' => [
BatchInterface::BATCH_ID => $batchId,
BatchInterface::STATUS => $status,
BatchInterface::CREATED_AT_DATE => $createdAtDate,
BatchInterface::UPDATED_AT_DATE => $updatedAtDate,
BatchInterface::INCLUDED_SHIPMENTS => $includedShipments,
BatchInterface::FAILED_SHIPMENTS => $failedShipments,
BatchInterface::DOCUMENTATION => $documentation,
]]);
return $batch;
}
}