GetLatLngFromAddress.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryDistanceBasedSourceSelection\Model\DistanceProvider\Offline;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\InventoryDistanceBasedSourceSelection\Model\Convert\AddressToString;
  10. use Magento\InventoryDistanceBasedSourceSelectionApi\Api\Data\LatLngInterface;
  11. use Magento\InventoryDistanceBasedSourceSelectionApi\Api\Data\LatLngInterfaceFactory;
  12. use Magento\InventoryDistanceBasedSourceSelectionApi\Api\GetLatLngFromAddressInterface;
  13. use Magento\InventoryDistanceBasedSourceSelection\Model\ResourceModel\GetGeoNameDataByAddress;
  14. use Magento\InventorySourceSelectionApi\Api\Data\AddressInterface;
  15. /**
  16. * @inheritdoc
  17. */
  18. class GetLatLngFromAddress implements GetLatLngFromAddressInterface
  19. {
  20. private $latLngCache = [];
  21. /**
  22. * @var LatLngInterfaceFactory
  23. */
  24. private $latLngInterfaceFactory;
  25. /**
  26. * @var GetGeoNameDataByAddress
  27. */
  28. private $getGeoNameDataByAddress;
  29. /**
  30. * @var AddressToString
  31. */
  32. private $addressToString;
  33. /**
  34. * GetLatLngFromAddress constructor.
  35. *
  36. * @param GetGeoNameDataByAddress $getGeoNameDataByAddress
  37. * @param LatLngInterfaceFactory $latLngInterfaceFactory
  38. * @param AddressToString $addressToString
  39. * @SuppressWarnings(PHPMD.LongVariable)
  40. */
  41. public function __construct(
  42. GetGeoNameDataByAddress $getGeoNameDataByAddress,
  43. LatLngInterfaceFactory $latLngInterfaceFactory,
  44. AddressToString $addressToString
  45. ) {
  46. $this->getGeoNameDataByAddress = $getGeoNameDataByAddress;
  47. $this->latLngInterfaceFactory = $latLngInterfaceFactory;
  48. $this->addressToString = $addressToString;
  49. }
  50. /**
  51. * @inheritdoc
  52. * @throws LocalizedException
  53. */
  54. public function execute(AddressInterface $address): LatLngInterface
  55. {
  56. $cacheKey = $this->addressToString->execute($address);
  57. if (!isset($this->latLngCache[$cacheKey])) {
  58. $geoNameData = $this->getGeoNameDataByAddress->execute($address);
  59. $this->latLngCache[$cacheKey] = $this->latLngInterfaceFactory->create([
  60. 'lat' => (float)$geoNameData['latitude'],
  61. 'lng' => (float)$geoNameData['longitude'],
  62. ]);
  63. }
  64. return $this->latLngCache[$cacheKey];
  65. }
  66. }