GetDistance.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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\InventoryDistanceBasedSourceSelectionApi\Api\Data\LatLngInterface;
  9. use Magento\InventoryDistanceBasedSourceSelectionApi\Api\GetDistanceInterface;
  10. /**
  11. * @inheritdoc
  12. */
  13. class GetDistance implements GetDistanceInterface
  14. {
  15. // Earth radius in kilometers used for distance calculation on earth surface between two points
  16. private const EARTH_RADIUS_KM = 6371000;
  17. /**
  18. * @inheritdoc
  19. */
  20. public function execute(LatLngInterface $source, LatLngInterface $destination): float
  21. {
  22. $latFrom = deg2rad($source->getLat());
  23. $lonFrom = deg2rad($source->getLng());
  24. $latTo = deg2rad($destination->getLat());
  25. $lonTo = deg2rad($destination->getLng());
  26. $latDelta = $latTo - $latFrom;
  27. $lonDelta = $lonTo - $lonFrom;
  28. $angle = 2 * asin(sqrt((sin($latDelta / 2) ** 2) +
  29. cos($latFrom) * cos($latTo) * (sin($lonDelta / 2) ** 2)));
  30. return $angle * (float) self::EARTH_RADIUS_KM;
  31. }
  32. }