DispatchResponseMapper.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Dispatch\ErrorInterfaceFactory;
  7. use Temando\Shipping\Model\Dispatch\PickupChargeInterface;
  8. use Temando\Shipping\Model\Dispatch\PickupChargeInterfaceFactory;
  9. use Temando\Shipping\Model\Dispatch\ShipmentInterface;
  10. use Temando\Shipping\Model\Dispatch\ShipmentInterfaceFactory;
  11. use Temando\Shipping\Model\DispatchInterface;
  12. use Temando\Shipping\Model\DispatchInterfaceFactory;
  13. use Temando\Shipping\Model\Shipment\ShipmentErrorInterface;
  14. use Temando\Shipping\Rest\Response\DataObject\Completion;
  15. use Temando\Shipping\Rest\Response\Fields\Completion\Group\Charge;
  16. use Temando\Shipping\Rest\Response\Fields\Completion\Shipment;
  17. /**
  18. * Map API data to application data object
  19. *
  20. * @package Temando\Shipping\Rest
  21. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  22. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link http://www.temando.com/
  25. */
  26. class DispatchResponseMapper
  27. {
  28. /**
  29. * @var DispatchInterfaceFactory
  30. */
  31. private $dispatchFactory;
  32. /**
  33. * @var ErrorInterfaceFactory
  34. */
  35. private $dispatchErrorFactory;
  36. /**
  37. * @var ShipmentInterfaceFactory
  38. */
  39. private $shipmentFactory;
  40. /**
  41. * @var PickupChargeInterfaceFactory
  42. */
  43. private $pickupChargeFactory;
  44. /**
  45. * @var DocumentationResponseMapper
  46. */
  47. private $documentationMapper;
  48. /**
  49. * DispatchResponseMapper constructor.
  50. * @param DispatchInterfaceFactory $dispatchFactory
  51. * @param ErrorInterfaceFactory $dispatchErrorFactory
  52. * @param ShipmentInterfaceFactory $shipmentFactory
  53. * @param PickupChargeInterfaceFactory $pickupChargeFactory
  54. * @param DocumentationResponseMapper $documentationMapper
  55. */
  56. public function __construct(
  57. DispatchInterfaceFactory $dispatchFactory,
  58. ErrorInterfaceFactory $dispatchErrorFactory,
  59. ShipmentInterfaceFactory $shipmentFactory,
  60. PickupChargeInterfaceFactory $pickupChargeFactory,
  61. DocumentationResponseMapper $documentationMapper
  62. ) {
  63. $this->dispatchFactory = $dispatchFactory;
  64. $this->dispatchErrorFactory = $dispatchErrorFactory;
  65. $this->shipmentFactory = $shipmentFactory;
  66. $this->pickupChargeFactory = $pickupChargeFactory;
  67. $this->documentationMapper = $documentationMapper;
  68. }
  69. /**
  70. * @param Shipment $apiShipment
  71. * @return ShipmentInterface
  72. */
  73. private function mapShipment(Shipment $apiShipment)
  74. {
  75. $errors = [];
  76. foreach ($apiShipment->getErrors() as $apiError) {
  77. $errors[]= $this->dispatchErrorFactory->create(['data' => [
  78. ShipmentErrorInterface::TITLE => $apiError->getTitle(),
  79. ShipmentErrorInterface::DETAIL => $apiError->getDetail(),
  80. ]]);
  81. }
  82. $shipment = $this->shipmentFactory->create(['data' => [
  83. ShipmentInterface::SHIPMENT_ID => $apiShipment->getId(),
  84. ShipmentInterface::STATUS => $apiShipment->getStatus(),
  85. ShipmentInterface::MESSAGE => $apiShipment->getMessage(),
  86. ShipmentInterface::ERRORS => $errors,
  87. ]]);
  88. return $shipment;
  89. }
  90. /**
  91. * @param Charge $apiCharge
  92. * @return PickupChargeInterface
  93. */
  94. private function mapPickupCharge(Charge $apiCharge)
  95. {
  96. $pickupCharge = $this->pickupChargeFactory->create(['data' => [
  97. PickupChargeInterface::DESCRIPTION => (string) $apiCharge->getDescription(),
  98. PickupChargeInterface::AMOUNT => (float) $apiCharge->getAmount()->getAmount(),
  99. PickupChargeInterface::CURRENCY => (string) $apiCharge->getAmount()->getCurrency(),
  100. ]]);
  101. return $pickupCharge;
  102. }
  103. /**
  104. * @param Completion $apiCompletion
  105. * @return DispatchInterface
  106. */
  107. public function map(Completion $apiCompletion)
  108. {
  109. $dispatchId = $apiCompletion->getId();
  110. $status = $apiCompletion->getAttributes()->getStatus();
  111. $customAttributes = $apiCompletion->getAttributes()->getCustomAttributes();
  112. $carrierName = $customAttributes ? $customAttributes->getCarrierName() : 'No Carrier Found';
  113. $createdAtDate = $apiCompletion->getAttributes()->getCreatedAt();
  114. $readyAtDate = $apiCompletion->getAttributes()->getReadyAt();
  115. $carrierMessages = [];
  116. $pickupReferences = [];
  117. $pickupCharges = [];
  118. $failedShipments = [];
  119. $includedShipments = [];
  120. $documentation = [];
  121. // split shipments into failed and successfully dispatched
  122. foreach ($apiCompletion->getAttributes()->getShipments() as $apiShipment) {
  123. if ($apiShipment->getStatus() === 'error') {
  124. $failedShipments[$apiShipment->getId()] = $this->mapShipment($apiShipment);
  125. } else {
  126. $includedShipments[$apiShipment->getId()] = $this->mapShipment($apiShipment);
  127. }
  128. }
  129. // map collected documentation
  130. foreach ($apiCompletion->getAttributes()->getGroups() as $attributeGroup) {
  131. $carrierMessages[]= $attributeGroup->getCarrierMessage();
  132. $pickupReferences[]= $attributeGroup->getPickupReference();
  133. foreach ($attributeGroup->getCharges() as $pickupCharge) {
  134. $pickupCharges[]= $this->mapPickupCharge($pickupCharge);
  135. }
  136. foreach ($attributeGroup->getDocumentation() as $apiDoc) {
  137. $documentation[]= $this->documentationMapper->map($apiDoc);
  138. }
  139. }
  140. $carrierMessages = array_filter($carrierMessages);
  141. $pickupReferences = array_filter($pickupReferences);
  142. sort($pickupReferences);
  143. $dispatch = $this->dispatchFactory->create(['data' => [
  144. DispatchInterface::DISPATCH_ID => $dispatchId,
  145. DispatchInterface::STATUS => $status,
  146. DispatchInterface::CARRIER_NAME => $carrierName,
  147. DispatchInterface::CARRIER_MESSAGES => $carrierMessages,
  148. DispatchInterface::CREATED_AT_DATE => $createdAtDate,
  149. DispatchInterface::READY_AT_DATE => $readyAtDate,
  150. DispatchInterface::PICKUP_NUMBERS => $pickupReferences,
  151. DispatchInterface::PICKUP_CHARGES => $pickupCharges,
  152. DispatchInterface::INCLUDED_SHIPMENTS => $includedShipments,
  153. DispatchInterface::FAILED_SHIPMENTS => $failedShipments,
  154. DispatchInterface::DOCUMENTATION => $documentation,
  155. ]]);
  156. return $dispatch;
  157. }
  158. }