GetLatLngFromSource.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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;
  8. use Magento\InventoryApi\Api\Data\SourceInterface;
  9. use Magento\InventorySourceSelectionApi\Api\Data\AddressInterfaceFactory;
  10. use Magento\InventoryDistanceBasedSourceSelectionApi\Api\Data\LatLngInterface;
  11. use Magento\InventoryDistanceBasedSourceSelectionApi\Api\GetLatLngFromAddressInterface;
  12. use Magento\InventoryDistanceBasedSourceSelectionApi\Api\Data\LatLngInterfaceFactory;
  13. /**
  14. * Class GetLatLngFromSource
  15. */
  16. class GetLatLngFromSource
  17. {
  18. /**
  19. * @var AddressInterfaceFactory
  20. */
  21. private $addressInterfaceFactory;
  22. /**
  23. * @var GetLatLngFromAddressInterface
  24. */
  25. private $getLatLngFromAddress;
  26. /**
  27. * @var LatLngInterfaceFactory
  28. */
  29. private $latLngInterfaceFactory;
  30. /**
  31. * GetAddressFromSource constructor.
  32. *
  33. * @param AddressInterfaceFactory $addressInterfaceFactory
  34. * @param LatLngInterfaceFactory $latLngInterfaceFactory
  35. * @param GetLatLngFromAddressInterface $getLatLngFromAddress
  36. */
  37. public function __construct(
  38. AddressInterfaceFactory $addressInterfaceFactory,
  39. LatLngInterfaceFactory $latLngInterfaceFactory,
  40. GetLatLngFromAddressInterface $getLatLngFromAddress
  41. ) {
  42. $this->addressInterfaceFactory = $addressInterfaceFactory;
  43. $this->getLatLngFromAddress = $getLatLngFromAddress;
  44. $this->latLngInterfaceFactory = $latLngInterfaceFactory;
  45. }
  46. /**
  47. * Get latitude and longitude from source
  48. *
  49. * @param SourceInterface $source
  50. * @return LatLngInterface
  51. */
  52. public function execute(SourceInterface $source): LatLngInterface
  53. {
  54. if (!$source->getLatitude() || !$source->getLongitude()) {
  55. $sourceAddress = $this->addressInterfaceFactory->create([
  56. 'country' => $source->getCountryId() ?? '',
  57. 'postcode' => $source->getPostcode() ?? '',
  58. 'street' => $source->getStreet() ?? '',
  59. 'region' => $source->getRegion() ?? '',
  60. 'city' => $source->getCity() ?? ''
  61. ]);
  62. return $this->getLatLngFromAddress->execute($sourceAddress);
  63. }
  64. return $this->latLngInterfaceFactory->create([
  65. 'lat' => (float) $source->getLatitude(),
  66. 'lng' => (float) $source->getLongitude()
  67. ]);
  68. }
  69. }