OrderResponseMapper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\ShipmentErrorInterface;
  7. use Temando\Shipping\Model\Shipment\ShipmentErrorInterfaceFactory;
  8. use Temando\Shipping\Model\ShipmentInterface;
  9. use Temando\Shipping\Rest\Response\DataObject\Shipment;
  10. use Temando\Shipping\Rest\Response\Document\SaveOrderInterface;
  11. use Temando\Shipping\Webservice\Response\Type\OrderResponseType;
  12. use Temando\Shipping\Webservice\Response\Type\OrderResponseTypeFactory;
  13. /**
  14. * Map API data to application data object
  15. *
  16. * @package Temando\Shipping\Rest
  17. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  18. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link http://www.temando.com/
  21. */
  22. class OrderResponseMapper
  23. {
  24. /**
  25. * @var OrderResponseTypeFactory
  26. */
  27. private $orderResponseFactory;
  28. /**
  29. * @var ShipmentResponseMapper
  30. */
  31. private $shipmentResponseMapper;
  32. /**
  33. * @var ShipmentErrorInterfaceFactory
  34. */
  35. private $shipmentErrorFactory;
  36. /**
  37. * OrderResponseMapper constructor.
  38. * @param OrderResponseTypeFactory $orderResponseFactory
  39. * @param ShipmentResponseMapper $shipmentResponseMapper
  40. * @param ShipmentErrorInterfaceFactory $shipmentErrorFactory
  41. */
  42. public function __construct(
  43. OrderResponseTypeFactory $orderResponseFactory,
  44. ShipmentResponseMapper $shipmentResponseMapper,
  45. ShipmentErrorInterfaceFactory $shipmentErrorFactory
  46. ) {
  47. $this->orderResponseFactory = $orderResponseFactory;
  48. $this->shipmentResponseMapper = $shipmentResponseMapper;
  49. $this->shipmentErrorFactory = $shipmentErrorFactory;
  50. }
  51. /**
  52. * @param Shipment[] $apiIncluded
  53. * @return ShipmentErrorInterface[]
  54. */
  55. private function mapErrors(array $apiIncluded)
  56. {
  57. /** @var Shipment[] $includedErrors */
  58. $includedErrors = array_filter($apiIncluded, function (Shipment $element) {
  59. return ($element->getType() == 'error');
  60. });
  61. $allocationErrors = [];
  62. foreach ($includedErrors as $item) {
  63. $allocationError = $this->shipmentErrorFactory->create(['data' => [
  64. ShipmentErrorInterface::TITLE => $item->getAttributes()->getTitle(),
  65. ShipmentErrorInterface::CODE => $item->getAttributes()->getCode(),
  66. ShipmentErrorInterface::STATUS => $item->getAttributes()->getStatus(),
  67. ShipmentErrorInterface::DETAIL => $item->getAttributes()->getDetail(),
  68. ]]);
  69. $allocationErrors[]= $allocationError;
  70. }
  71. return $allocationErrors;
  72. }
  73. /**
  74. * @param Shipment[] $apiIncluded
  75. * @return ShipmentInterface[]
  76. */
  77. private function mapShipments(array $apiIncluded)
  78. {
  79. /** @var Shipment[] $includedShipments */
  80. $includedShipments = array_filter($apiIncluded, function (Shipment $element) {
  81. return ($element->getType() == 'shipment');
  82. });
  83. $shipments = [];
  84. foreach ($includedShipments as $shipment) {
  85. $shipments[]= $this->shipmentResponseMapper->map($shipment);
  86. }
  87. return $shipments;
  88. }
  89. /**
  90. * @param SaveOrderInterface $apiOrder
  91. * @return OrderResponseType
  92. */
  93. public function map(SaveOrderInterface $apiOrder)
  94. {
  95. $orderId = $apiOrder->getData()->getId();
  96. $shipments = $this->mapShipments($apiOrder->getIncluded());
  97. $errors = $this->mapErrors($apiOrder->getIncluded());
  98. $orderResponse = $this->orderResponseFactory->create([
  99. 'orderId' => $orderId,
  100. 'errors' => $errors,
  101. 'shipments' => $shipments,
  102. ]);
  103. return $orderResponse;
  104. }
  105. }