DeliveryLocationResponseMapper.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Api\Data\Delivery\QuoteCollectionPointInterface;
  8. use Temando\Shipping\Api\Data\Delivery\QuoteCollectionPointInterfaceFactory;
  9. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  10. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterfaceFactory;
  11. use Temando\Shipping\Api\Data\Order\ShippingExperienceInterface;
  12. use Temando\Shipping\Rest\Response\DataObject\OrderQualification;
  13. use Temando\Shipping\Rest\Response\DataObject\CollectionPoint;
  14. use Temando\Shipping\Rest\Response\DataObject\Location;
  15. use Temando\Shipping\Rest\Response\Fields\Generic\Value;
  16. use Temando\Shipping\Rest\Response\Fields\Location\OpeningHours;
  17. use Magento\Shipping\Helper\Carrier;
  18. /**
  19. * Map API data to application data object
  20. *
  21. * @package Temando\Shipping\Rest
  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 DeliveryLocationResponseMapper
  27. {
  28. /**
  29. * @var OpeningHoursMapper
  30. */
  31. private $openingHoursMapper;
  32. /**
  33. * @var ShippingExperienceMapper
  34. */
  35. private $shippingExperienceMapper;
  36. /**
  37. * @var Carrier
  38. */
  39. private $unitConverter;
  40. /**
  41. * @var QuoteCollectionPointInterfaceFactory
  42. */
  43. private $collectionPointFactory;
  44. /**
  45. * @var QuotePickupLocationInterfaceFactory
  46. */
  47. private $pickupLocationFactory;
  48. /**
  49. * DeliveryLocationResponseMapper constructor.
  50. * @param OpeningHoursMapper $openingHoursMapper
  51. * @param ShippingExperienceMapper $shippingExperienceMapper
  52. * @param Carrier $unitConverter
  53. * @param QuoteCollectionPointInterfaceFactory $collectionPointFactory
  54. * @param QuotePickupLocationInterfaceFactory $pickupLocationFactory
  55. */
  56. public function __construct(
  57. OpeningHoursMapper $openingHoursMapper,
  58. ShippingExperienceMapper $shippingExperienceMapper,
  59. Carrier $unitConverter,
  60. QuoteCollectionPointInterfaceFactory $collectionPointFactory,
  61. QuotePickupLocationInterfaceFactory $pickupLocationFactory
  62. ) {
  63. $this->openingHoursMapper = $openingHoursMapper;
  64. $this->shippingExperienceMapper = $shippingExperienceMapper;
  65. $this->unitConverter = $unitConverter;
  66. $this->collectionPointFactory = $collectionPointFactory;
  67. $this->pickupLocationFactory = $pickupLocationFactory;
  68. }
  69. /**
  70. * Normalize delivery location distance to meters.
  71. *
  72. * @param Value|null $apiDistance
  73. * @return int|null
  74. */
  75. private function mapDistance(?Value $apiDistance): ?int
  76. {
  77. if (!$apiDistance instanceof Value) {
  78. return null;
  79. }
  80. $targetUnit = \Zend_Measure_Length::METER;
  81. $sourceValue = $apiDistance->getValue();
  82. switch ($apiDistance->getUnit()) {
  83. case 'mi':
  84. $sourceUnit = \Zend_Measure_Length::MILE;
  85. break;
  86. default:
  87. $sourceUnit = \Zend_Measure_Length::KILOMETER;
  88. }
  89. $value = (int) $this->unitConverter->convertMeasureDimension($sourceValue, $sourceUnit, $targetUnit);
  90. return $value;
  91. }
  92. /**
  93. * Transform opening hours format.
  94. *
  95. * @param OpeningHours|null $apiHours
  96. * @return string[][]
  97. */
  98. private function mapOpeningHours(OpeningHours $apiHours = null): array
  99. {
  100. if ($apiHours instanceof OpeningHours) {
  101. return $this->openingHoursMapper->map($apiHours);
  102. } else {
  103. return [
  104. 'general' => [],
  105. 'specific' => [],
  106. ];
  107. }
  108. }
  109. /**
  110. * @param OrderQualification[] $apiQualifications
  111. * @return ShippingExperienceInterface[]
  112. */
  113. private function mapShippingExperiences(array $apiQualifications): array
  114. {
  115. $shippingExperiences = [];
  116. foreach ($apiQualifications as $apiQualification) {
  117. try {
  118. $shippingExperiences[]= $this->shippingExperienceMapper->map($apiQualification);
  119. } catch (LocalizedException $exception) {
  120. continue;
  121. }
  122. }
  123. return $shippingExperiences;
  124. }
  125. /**
  126. * Create collection point object from platform data.
  127. *
  128. * @param CollectionPoint $apiCollectionPoint
  129. * @param OrderQualification[] $apiQualifications
  130. * @return QuoteCollectionPointInterface
  131. */
  132. public function mapCollectionPoint(
  133. CollectionPoint $apiCollectionPoint,
  134. array $apiQualifications
  135. ): QuoteCollectionPointInterface {
  136. $apiLocation = $apiCollectionPoint->getAttributes()->getLocation();
  137. $apiAddress = $apiLocation->getAddress();
  138. $distance = $this->mapDistance($apiCollectionPoint->getAttributes()->getDistance());
  139. $openingHours = $this->mapOpeningHours($apiLocation->getOpeningHours());
  140. $shippingExperiences = $this->mapShippingExperiences($apiQualifications);
  141. $collectionPoint = $this->collectionPointFactory->create(['data' => [
  142. QuoteCollectionPointInterface::COLLECTION_POINT_ID => $apiCollectionPoint->getId(),
  143. QuoteCollectionPointInterface::NAME => $apiCollectionPoint->getAttributes()->getName(),
  144. QuoteCollectionPointInterface::COUNTRY => $apiAddress->getCountryCode(),
  145. QuoteCollectionPointInterface::REGION => $apiAddress->getAdministrativeArea(),
  146. QuoteCollectionPointInterface::POSTCODE => $apiAddress->getPostalCode(),
  147. QuoteCollectionPointInterface::CITY => $apiAddress->getLocality(),
  148. QuoteCollectionPointInterface::STREET => $apiAddress->getLines(),
  149. QuoteCollectionPointInterface::DISTANCE => $distance,
  150. QuoteCollectionPointInterface::OPENING_HOURS => $openingHours['general'],
  151. QuoteCollectionPointInterface::SHIPPING_EXPERIENCES => $shippingExperiences,
  152. ]]);
  153. return $collectionPoint;
  154. }
  155. /**
  156. * Create pickup location object from platform data.
  157. *
  158. * @param Location $apiPickupLocation
  159. * @param OrderQualification[] $apiQualifications
  160. * @return QuotePickupLocationInterface
  161. */
  162. public function mapPickupLocation(
  163. Location $apiPickupLocation,
  164. array $apiQualifications
  165. ): QuotePickupLocationInterface {
  166. $apiLocation = $apiPickupLocation->getAttributes();
  167. $apiAddress = $apiPickupLocation->getAttributes()->getAddress();
  168. $distance = null; // no distance information available for click&collect locations yet
  169. $openingHours = $this->mapOpeningHours($apiLocation->getOpeningHours());
  170. $shippingExperiences = $this->mapShippingExperiences($apiQualifications);
  171. $pickupLocation = $this->pickupLocationFactory->create(['data' => [
  172. QuotePickupLocationInterface::PICKUP_LOCATION_ID => $apiPickupLocation->getId(),
  173. QuotePickupLocationInterface::NAME => $apiLocation->getName(),
  174. QuotePickupLocationInterface::COUNTRY => $apiAddress->getCountryCode(),
  175. QuotePickupLocationInterface::REGION => $apiAddress->getAdministrativeArea(),
  176. QuotePickupLocationInterface::POSTCODE => $apiAddress->getPostalCode(),
  177. QuotePickupLocationInterface::CITY => $apiAddress->getLocality(),
  178. QuotePickupLocationInterface::STREET => $apiAddress->getLines(),
  179. QuotePickupLocationInterface::DISTANCE => $distance,
  180. QuotePickupLocationInterface::OPENING_HOURS => $openingHours,
  181. QuotePickupLocationInterface::SHIPPING_EXPERIENCES => $shippingExperiences,
  182. ]]);
  183. return $pickupLocation;
  184. }
  185. }