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
<?php
/**
* Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
*/
namespace Temando\Shipping\Rest\EntityMapper;
use Temando\Shipping\Model\Shipment\PackageInterface;
use Temando\Shipping\Model\Shipment\PackageInterfaceFactory;
use Temando\Shipping\Model\Shipment\PackageItemInterface;
use Temando\Shipping\Model\Shipment\PackageItemInterfaceFactory;
use Temando\Shipping\Rest\Response\Fields\Generic\Item;
use Temando\Shipping\Rest\Response\Fields\Generic\Package;
/**
* Map API data to application data object
*
* @package Temando\Shipping\Rest
* @author Rhodri Davies <rhodri.davies@temando.com>
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.temando.com
*/
class PackageResponseMapper
{
/**
* @var PackageInterfaceFactory
*/
private $packageFactory;
/**
* @var PackageItemInterfaceFactory
*/
private $packageItemFactory;
/**
* PackageResponseMapper constructor.
* @param PackageInterfaceFactory $packageFactory
* @param PackageItemInterfaceFactory $packageItemFactory
*/
public function __construct(
PackageInterfaceFactory $packageFactory,
PackageItemInterfaceFactory $packageItemFactory
) {
$this->packageFactory = $packageFactory;
$this->packageItemFactory = $packageItemFactory;
}
/**
* @param Item[] $apiPackageItems
* @return PackageItemInterface[]
*/
public function mapItems(array $apiPackageItems)
{
$packageItems = [];
foreach ($apiPackageItems as $apiPackageItem) {
$apiProduct = $apiPackageItem->getProduct();
$weight = $apiProduct->getWeight();
$monetaryValue = $apiProduct->getMonetaryValue();
$packageItemData = [
PackageItemInterface::SKU => $apiProduct->getSku(),
PackageItemInterface::DESCRIPTION => $apiProduct->getDescription(),
PackageItemInterface::QTY => $apiPackageItem->getQuantity(),
PackageItemInterface::UNIT => $apiProduct->getUnit(),
];
if ($classificationCodes = $apiProduct->getClassificationCodes()) {
if ($hsCode = $classificationCodes->getHsCode()) {
$packageItemData[PackageItemInterface::HS_CODE] = $hsCode;
}
}
if ($manufacture = $apiProduct->getManufacture()) {
if ($manufactureAddress = $manufacture->getAddress()) {
if ($manufactureCountryCode = $manufactureAddress->getCountryCode()) {
$packageItemData[PackageItemInterface::COUNTRY_OF_MANUFACTURE] = $manufactureCountryCode;
}
}
}
if ($origin = $apiProduct->getOrigin()) {
if ($originAddress = $origin->getAddress()) {
if ($originCountryCode = $originAddress->getCountryCode()) {
$packageItemData[PackageItemInterface::COUNTRY_OF_ORIGIN] = $originCountryCode;
}
}
}
if ($weight) {
$packageItemData[PackageItemInterface::WEIGHT] = sprintf(
'%s %s',
$weight->getValue(),
$weight->getUnit()
);
}
if ($monetaryValue) {
$packageItemData[PackageItemInterface::MONETARY_VALUE] = sprintf(
'%s %s',
$monetaryValue->getAmount(),
$monetaryValue->getCurrency()
);
}
$packageItemFactoryData = ['data' => $packageItemData];
/** @var \Temando\Shipping\Model\Shipment\PackageItem $packageItem */
$packageItem = $this->packageItemFactory->create($packageItemFactoryData);
$packageItems[]= $packageItem;
}
return $packageItems;
}
/**
* @param Package $apiPackage
* @return PackageInterface
*/
public function map(Package $apiPackage)
{
$dimensions = $apiPackage->getDimensions();
$grossWeight = $apiPackage->getGrossWeight();
$items = $this->mapItems($apiPackage->getItems());
/** @var \Temando\Shipping\Model\Shipment\Package $package */
$package = $this->packageFactory->create(['data' => [
PackageInterface::PACKAGE_ID => $apiPackage->getId(),
PackageInterface::TRACKING_REFERENCE => $apiPackage->getTrackingReference(),
PackageInterface::TRACKING_URL => $apiPackage->getTrackingUrl(),
PackageInterface::WEIGHT => sprintf(
'%s %s',
$grossWeight->getValue(),
$grossWeight->getUnit()
),
PackageInterface::WIDTH => sprintf('%s %s', $dimensions->getWidth(), $dimensions->getUnit()),
PackageInterface::LENGTH => sprintf('%s %s', $dimensions->getLength(), $dimensions->getUnit()),
PackageInterface::HEIGHT => sprintf('%s %s', $dimensions->getHeight(), $dimensions->getUnit()),
PackageInterface::ITEMS => $items
]]);
return $package;
}
}