FulfillmentResponseMapper.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Framework\Exception\LocalizedException;
  7. use Temando\Shipping\Model\PickupInterface;
  8. use Temando\Shipping\Model\PickupInterfaceFactory;
  9. use Temando\Shipping\Model\Shipment\LocationInterface;
  10. use Temando\Shipping\Model\Shipment\LocationInterfaceFactory;
  11. use Temando\Shipping\Rest\Response\DataObject\Fulfillment;
  12. use Temando\Shipping\Rest\Response\Fields\Fulfillment\Item;
  13. use Temando\Shipping\Rest\Response\Fields\Location\OpeningHours;
  14. use Temando\Shipping\Rest\Response\Fields\LocationAttributes;
  15. use Temando\Shipping\Rest\Response\Fields\Relationship;
  16. /**
  17. * Map API data to application data object
  18. *
  19. * @package Temando\Shipping\Rest
  20. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  21. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  22. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. * @link http://www.temando.com/
  24. */
  25. class FulfillmentResponseMapper implements PointerAwareInterface
  26. {
  27. /**
  28. * @var LocationInterfaceFactory
  29. */
  30. private $locationFactory;
  31. /**
  32. * @var PickupInterfaceFactory
  33. */
  34. private $pickupFactory;
  35. /**
  36. * @var OpeningHoursMapper
  37. */
  38. private $openingHoursMapper;
  39. /**
  40. * FulfillmentResponseMapper constructor.
  41. * @param LocationInterfaceFactory $locationFactory
  42. * @param PickupInterfaceFactory $pickupFactory
  43. * @param OpeningHoursMapper $openingHoursMapper
  44. */
  45. public function __construct(
  46. LocationInterfaceFactory $locationFactory,
  47. PickupInterfaceFactory $pickupFactory,
  48. OpeningHoursMapper $openingHoursMapper
  49. ) {
  50. $this->locationFactory = $locationFactory;
  51. $this->pickupFactory = $pickupFactory;
  52. $this->openingHoursMapper = $openingHoursMapper;
  53. }
  54. /**
  55. * @param Relationship[] $relationships
  56. * @return string
  57. */
  58. private function extractOrderId(array $relationships)
  59. {
  60. foreach ($relationships as $relationship) {
  61. foreach ($relationship->getData() as $resourceIdentifier) {
  62. if ($resourceIdentifier->getType() === 'order') {
  63. return $resourceIdentifier->getId();
  64. }
  65. };
  66. }
  67. return '';
  68. }
  69. /**
  70. * @param LocationAttributes $apiLocation
  71. * @return string[][]
  72. */
  73. private function mapOpeningHours(LocationAttributes $apiLocation)
  74. {
  75. if ($apiLocation->getOpeningHours() instanceof OpeningHours) {
  76. return $this->openingHoursMapper->map($apiLocation->getOpeningHours());
  77. } else {
  78. return [
  79. 'general' => [],
  80. 'specific' => [],
  81. ];
  82. }
  83. }
  84. /**
  85. * @param LocationAttributes $apiLocation
  86. * @return LocationInterface
  87. */
  88. private function mapLocation(LocationAttributes $apiLocation)
  89. {
  90. $contact = $apiLocation->getContact();
  91. $openingHours = $this->mapOpeningHours($apiLocation);
  92. $location = $this->locationFactory->create(['data' => [
  93. LocationInterface::NAME => (string)$apiLocation->getName(),
  94. LocationInterface::COMPANY => $contact ? $contact->getOrganisationName() : '',
  95. LocationInterface::PERSON_FIRST_NAME => $contact ? $contact->getPersonFirstName() : '',
  96. LocationInterface::PERSON_LAST_NAME => $contact ? $contact->getPersonLastName() : '',
  97. LocationInterface::EMAIL => $contact ? $contact->getEmail() : '',
  98. LocationInterface::PHONE_NUMBER => $contact ? $contact->getPhoneNumber() : '',
  99. LocationInterface::STREET => $apiLocation->getAddress()->getLines(),
  100. LocationInterface::CITY => $apiLocation->getAddress()->getLocality(),
  101. LocationInterface::POSTAL_CODE => $apiLocation->getAddress()->getPostalCode(),
  102. LocationInterface::REGION_CODE => $apiLocation->getAddress()->getAdministrativeArea(),
  103. LocationInterface::COUNTRY_CODE => $apiLocation->getAddress()->getCountryCode(),
  104. LocationInterface::TYPE => (string)$apiLocation->getType(),
  105. LocationInterface::OPENING_HOURS => $openingHours,
  106. ]]);
  107. return $location;
  108. }
  109. /**
  110. * @param Item[] $apiItems
  111. * @return string[]
  112. */
  113. private function mapPickupItems(array $apiItems)
  114. {
  115. $items = [];
  116. foreach ($apiItems as $apiItem) {
  117. $items[$apiItem->getProduct()->getSku()] = $apiItem->getQuantity();
  118. }
  119. return $items;
  120. }
  121. /**
  122. * @param Fulfillment $apiFulfillment
  123. * @return PickupInterface
  124. */
  125. public function mapPickup(Fulfillment $apiFulfillment)
  126. {
  127. $orderId = $this->extractOrderId($apiFulfillment->getRelationships());
  128. /** @var \Magento\Sales\Model\Order\Address $shippingAddress */
  129. $items = $this->mapPickupItems($apiFulfillment->getAttributes()->getItems());
  130. $pickupLocation = $this->mapLocation($apiFulfillment->getAttributes()->getPickUpLocation());
  131. $pickup = $this->pickupFactory->create(['data' => [
  132. PickupInterface::PICKUP_ID => $apiFulfillment->getId(),
  133. PickupInterface::STATE => $apiFulfillment->getAttributes()->getState(),
  134. PickupInterface::ORDER_ID => $orderId,
  135. PickupInterface::ORDER_REFERENCE => $apiFulfillment->getAttributes()->getReference(),
  136. PickupInterface::CREATED_AT => $apiFulfillment->getAttributes()->getCreatedAt(),
  137. PickupInterface::READY_AT => $apiFulfillment->getAttributes()->getReadyAt(),
  138. PickupInterface::PICKUP_LOCATION => $pickupLocation,
  139. PickupInterface::ITEMS => $items,
  140. ]]);
  141. return $pickup;
  142. }
  143. /**
  144. * Obtain the JSON pointer to a platform property that corresponds to a local entity property.
  145. *
  146. * @param string $property The local property name
  147. * @return string The platform property pointer
  148. * @throws LocalizedException
  149. */
  150. public function getPath($property)
  151. {
  152. $pathMap = [
  153. PickupInterface::CREATED_AT => '/createdAt',
  154. PickupInterface::READY_AT => '/readyAt',
  155. PickupInterface::LOCATION_ID => '/pickUpLocation',
  156. PickupInterface::ORDER_ID => '/order',
  157. PickupInterface::STATE => '/state',
  158. ];
  159. if (!isset($pathMap[$property])) {
  160. throw new LocalizedException(__('Filter field %1 is not supported by the API.', $property));
  161. }
  162. return $pathMap[$property];
  163. }
  164. }