BatchResponseMapper.php 6.8 KB

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