PackageResponseMapper.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\EntityMapper;
  6. use Temando\Shipping\Model\Shipment\PackageInterface;
  7. use Temando\Shipping\Model\Shipment\PackageInterfaceFactory;
  8. use Temando\Shipping\Model\Shipment\PackageItemInterface;
  9. use Temando\Shipping\Model\Shipment\PackageItemInterfaceFactory;
  10. use Temando\Shipping\Rest\Response\Fields\Generic\Item;
  11. use Temando\Shipping\Rest\Response\Fields\Generic\Package;
  12. /**
  13. * Map API data to application data object
  14. *
  15. * @package Temando\Shipping\Rest
  16. * @author Rhodri Davies <rhodri.davies@temando.com>
  17. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link https://www.temando.com
  19. */
  20. class PackageResponseMapper
  21. {
  22. /**
  23. * @var PackageInterfaceFactory
  24. */
  25. private $packageFactory;
  26. /**
  27. * @var PackageItemInterfaceFactory
  28. */
  29. private $packageItemFactory;
  30. /**
  31. * PackageResponseMapper constructor.
  32. * @param PackageInterfaceFactory $packageFactory
  33. * @param PackageItemInterfaceFactory $packageItemFactory
  34. */
  35. public function __construct(
  36. PackageInterfaceFactory $packageFactory,
  37. PackageItemInterfaceFactory $packageItemFactory
  38. ) {
  39. $this->packageFactory = $packageFactory;
  40. $this->packageItemFactory = $packageItemFactory;
  41. }
  42. /**
  43. * @param Item[] $apiPackageItems
  44. * @return PackageItemInterface[]
  45. */
  46. public function mapItems(array $apiPackageItems)
  47. {
  48. $packageItems = [];
  49. foreach ($apiPackageItems as $apiPackageItem) {
  50. $apiProduct = $apiPackageItem->getProduct();
  51. $weight = $apiProduct->getWeight();
  52. $monetaryValue = $apiProduct->getMonetaryValue();
  53. $packageItemData = [
  54. PackageItemInterface::SKU => $apiProduct->getSku(),
  55. PackageItemInterface::DESCRIPTION => $apiProduct->getDescription(),
  56. PackageItemInterface::QTY => $apiPackageItem->getQuantity(),
  57. PackageItemInterface::UNIT => $apiProduct->getUnit(),
  58. ];
  59. if ($classificationCodes = $apiProduct->getClassificationCodes()) {
  60. if ($hsCode = $classificationCodes->getHsCode()) {
  61. $packageItemData[PackageItemInterface::HS_CODE] = $hsCode;
  62. }
  63. }
  64. if ($manufacture = $apiProduct->getManufacture()) {
  65. if ($manufactureAddress = $manufacture->getAddress()) {
  66. if ($manufactureCountryCode = $manufactureAddress->getCountryCode()) {
  67. $packageItemData[PackageItemInterface::COUNTRY_OF_MANUFACTURE] = $manufactureCountryCode;
  68. }
  69. }
  70. }
  71. if ($origin = $apiProduct->getOrigin()) {
  72. if ($originAddress = $origin->getAddress()) {
  73. if ($originCountryCode = $originAddress->getCountryCode()) {
  74. $packageItemData[PackageItemInterface::COUNTRY_OF_ORIGIN] = $originCountryCode;
  75. }
  76. }
  77. }
  78. if ($weight) {
  79. $packageItemData[PackageItemInterface::WEIGHT] = sprintf(
  80. '%s %s',
  81. $weight->getValue(),
  82. $weight->getUnit()
  83. );
  84. }
  85. if ($monetaryValue) {
  86. $packageItemData[PackageItemInterface::MONETARY_VALUE] = sprintf(
  87. '%s %s',
  88. $monetaryValue->getAmount(),
  89. $monetaryValue->getCurrency()
  90. );
  91. }
  92. $packageItemFactoryData = ['data' => $packageItemData];
  93. /** @var \Temando\Shipping\Model\Shipment\PackageItem $packageItem */
  94. $packageItem = $this->packageItemFactory->create($packageItemFactoryData);
  95. $packageItems[]= $packageItem;
  96. }
  97. return $packageItems;
  98. }
  99. /**
  100. * @param Package $apiPackage
  101. * @return PackageInterface
  102. */
  103. public function map(Package $apiPackage)
  104. {
  105. $dimensions = $apiPackage->getDimensions();
  106. $grossWeight = $apiPackage->getGrossWeight();
  107. $items = $this->mapItems($apiPackage->getItems());
  108. /** @var \Temando\Shipping\Model\Shipment\Package $package */
  109. $package = $this->packageFactory->create(['data' => [
  110. PackageInterface::PACKAGE_ID => $apiPackage->getId(),
  111. PackageInterface::TRACKING_REFERENCE => $apiPackage->getTrackingReference(),
  112. PackageInterface::TRACKING_URL => $apiPackage->getTrackingUrl(),
  113. PackageInterface::WEIGHT => sprintf(
  114. '%s %s',
  115. $grossWeight->getValue(),
  116. $grossWeight->getUnit()
  117. ),
  118. PackageInterface::WIDTH => sprintf('%s %s', $dimensions->getWidth(), $dimensions->getUnit()),
  119. PackageInterface::LENGTH => sprintf('%s %s', $dimensions->getLength(), $dimensions->getUnit()),
  120. PackageInterface::HEIGHT => sprintf('%s %s', $dimensions->getHeight(), $dimensions->getUnit()),
  121. PackageInterface::ITEMS => $items
  122. ]]);
  123. return $package;
  124. }
  125. }