DistanceConverter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Delivery;
  6. use Magento\Directory\Helper\Data;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Shipping\Helper\Carrier;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Temando Delivery Location Distance Converter
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class DistanceConverter
  19. {
  20. /**
  21. * @var ScopeConfigInterface
  22. */
  23. private $scopeConfig;
  24. /**
  25. * @var Carrier
  26. */
  27. private $unitConverter;
  28. /**
  29. * DistanceConverter constructor.
  30. * @param ScopeConfigInterface $scopeConfig
  31. * @param Carrier $unitConverter
  32. */
  33. public function __construct(
  34. ScopeConfigInterface $scopeConfig,
  35. Carrier $unitConverter
  36. ) {
  37. $this->scopeConfig = $scopeConfig;
  38. $this->unitConverter = $unitConverter;
  39. }
  40. /**
  41. * Normalize a delivery location distance to meters.
  42. *
  43. * @param float $value
  44. * @param string $unit "km" or "mi"
  45. *
  46. * @return int
  47. * @throws \InvalidArgumentException
  48. */
  49. public function normalize(float $value, string $unit): int
  50. {
  51. $targetUnit = \Zend_Measure_Length::METER;
  52. switch ($unit) {
  53. case 'mi':
  54. $sourceUnit = \Zend_Measure_Length::MILE;
  55. break;
  56. case 'km':
  57. $sourceUnit = \Zend_Measure_Length::KILOMETER;
  58. break;
  59. default:
  60. throw new \InvalidArgumentException("Unit $unit is not supported for delivery location distance.");
  61. }
  62. $value = (int) $this->unitConverter->convertMeasureDimension($value, $sourceUnit, $targetUnit);
  63. return $value;
  64. }
  65. /**
  66. * Localize a delivery location distance.
  67. *
  68. * @param int $value
  69. * @param int $storeId
  70. * @return float
  71. */
  72. public function localize(int $value, int $storeId): float
  73. {
  74. $sourceUnit = \Zend_Measure_Length::METER;
  75. $weightUnit = $this->scopeConfig->getValue(Data::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE, $storeId);
  76. $targetUnit = ($weightUnit === 'kgs') ? \Zend_Measure_Length::KILOMETER : \Zend_Measure_Length::MILE;
  77. $value = (float) $this->unitConverter->convertMeasureDimension($value, $sourceUnit, $targetUnit);
  78. return $value;
  79. }
  80. /**
  81. * Localize a delivery location distance and obtain display text incl. unit.
  82. *
  83. * @param int|null $value
  84. * @param int $storeId
  85. * @param string $format
  86. * @param int $precision
  87. * @return string
  88. */
  89. public function format(?int $value, int $storeId, string $format = '%1$s %2$s', int $precision = 3): string
  90. {
  91. if (!$value) {
  92. return (string) $value;
  93. }
  94. $value = $this->localize($value, $storeId);
  95. $value = (string) round($value, $precision);
  96. $weightUnit = $this->scopeConfig->getValue(Data::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE, $storeId);
  97. $targetUnit = ($weightUnit === 'kgs') ? \Zend_Measure_Length::KILOMETER : \Zend_Measure_Length::MILE;
  98. $unit = $this->unitConverter->getMeasureDimensionName($targetUnit);
  99. return sprintf($format, $value, $unit);
  100. }
  101. }