diff --git a/InventoryDistanceBasedSourceSelection/Plugin/InventoryApi/SourceRepository/Adminhtml/FillSourceLatitudeAndLongitude.php b/InventoryDistanceBasedSourceSelection/Plugin/InventoryApi/SourceRepository/Adminhtml/FillSourceLatitudeAndLongitude.php
new file mode 100644
index 000000000000..92b822a2d5d9
--- /dev/null
+++ b/InventoryDistanceBasedSourceSelection/Plugin/InventoryApi/SourceRepository/Adminhtml/FillSourceLatitudeAndLongitude.php
@@ -0,0 +1,85 @@
+getLatLngFromSource = $getLatLngFromSource;
+ $this->logger = $logger;
+ $this->messageManager = $messageManager;
+ }
+
+ /**
+ * Calculate latitude and longitude using google map if api key is defined
+ *
+ * @param SourceRepositoryInterface $subject
+ * @param SourceInterface $source
+ * @return array
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ */
+ public function beforeSave(
+ SourceRepositoryInterface $subject,
+ SourceInterface $source
+ ): array {
+ if (!$source->getLatitude() && !$source->getLongitude()) {
+ try {
+ $latLng = $this->getLatLngFromSource->execute($source);
+
+ $source->setLatitude($latLng->getLat());
+ $source->setLongitude($latLng->getLng());
+ } catch (LocalizedException $exception) {
+ $this->logger->error($exception);
+ $this->messageManager->addWarningMessage($exception->getMessage());
+ } catch (\Exception $exception) {
+ $this->logger->error($exception);
+ $this->messageManager->addWarningMessage(__('Failed to geocode the source address'));
+ }
+ }
+
+ return [$source];
+ }
+}
diff --git a/InventoryDistanceBasedSourceSelection/Plugin/FillSourceLatitudeAndLongitude.php b/InventoryDistanceBasedSourceSelection/Plugin/InventoryApi/SourceRepository/FillSourceLatitudeAndLongitude.php
similarity index 77%
rename from InventoryDistanceBasedSourceSelection/Plugin/FillSourceLatitudeAndLongitude.php
rename to InventoryDistanceBasedSourceSelection/Plugin/InventoryApi/SourceRepository/FillSourceLatitudeAndLongitude.php
index ffbda77eb729..b5f10d144d8a 100644
--- a/InventoryDistanceBasedSourceSelection/Plugin/FillSourceLatitudeAndLongitude.php
+++ b/InventoryDistanceBasedSourceSelection/Plugin/InventoryApi/SourceRepository/FillSourceLatitudeAndLongitude.php
@@ -6,12 +6,12 @@
declare(strict_types=1);
-namespace Magento\InventoryDistanceBasedSourceSelection\Plugin;
+namespace Magento\InventoryDistanceBasedSourceSelection\Plugin\InventoryApi\SourceRepository;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryDistanceBasedSourceSelection\Model\DistanceProvider\GetLatLngFromSource;
-use Magento\InventoryDistanceBasedSourceSelectionApi\Model\GetLatLngFromSourceInterface;
+use Psr\Log\LoggerInterface;
/**
* Compute latitude and longitude for a source if none is defined
@@ -23,16 +23,24 @@ class FillSourceLatitudeAndLongitude
*/
private $getLatLngFromSource;
+ /**
+ * @var LoggerInterface
+ */
+ private $logger;
+
/**
* ComputeSourceLatitudeAndLongitude constructor.
*
* @param GetLatLngFromSource $getLatLngFromSource
+ * @param LoggerInterface $logger
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
- GetLatLngFromSource $getLatLngFromSource
+ GetLatLngFromSource $getLatLngFromSource,
+ LoggerInterface $logger
) {
$this->getLatLngFromSource = $getLatLngFromSource;
+ $this->logger = $logger;
}
/**
@@ -53,8 +61,8 @@ public function beforeSave(
$source->setLatitude($latLng->getLat());
$source->setLongitude($latLng->getLng());
- } catch (\Exception $e) {
- unset($e); // Silently fail geo coding
+ } catch (\Exception $exception) {
+ $this->logger->error($exception);
}
}
diff --git a/InventoryDistanceBasedSourceSelection/etc/adminhtml/di.xml b/InventoryDistanceBasedSourceSelection/etc/adminhtml/di.xml
new file mode 100644
index 000000000000..270f9ee9e542
--- /dev/null
+++ b/InventoryDistanceBasedSourceSelection/etc/adminhtml/di.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
diff --git a/InventoryDistanceBasedSourceSelection/etc/di.xml b/InventoryDistanceBasedSourceSelection/etc/di.xml
index 98d07b326aee..d1333342a22d 100644
--- a/InventoryDistanceBasedSourceSelection/etc/di.xml
+++ b/InventoryDistanceBasedSourceSelection/etc/di.xml
@@ -60,7 +60,7 @@
+ type="Magento\InventoryDistanceBasedSourceSelection\Plugin\InventoryApi\SourceRepository\FillSourceLatitudeAndLongitude"/>