Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\InventoryDistanceBasedSourceSelection\Plugin\InventoryApi\SourceRepository\Adminhtml;

use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryDistanceBasedSourceSelection\Model\DistanceProvider\GetLatLngFromSource;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;

/**
* Compute latitude and longitude for a source if none is defined
*/
class FillSourceLatitudeAndLongitude
{
/**
* @var GetLatLngFromSource
*/
private $getLatLngFromSource;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var ManagerInterface
*/
private $messageManager;

/**
* ComputeSourceLatitudeAndLongitude constructor.
*
* @param GetLatLngFromSource $getLatLngFromSource
* @param LoggerInterface $logger
* @param ManagerInterface $messageManager
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
GetLatLngFromSource $getLatLngFromSource,
LoggerInterface $logger,
ManagerInterface $messageManager
) {
$this->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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}

Expand Down
14 changes: 14 additions & 0 deletions InventoryDistanceBasedSourceSelection/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\InventoryApi\Api\SourceRepositoryInterface">
<plugin sortOrder="10" name="updateSourceLatitudeAndLongitude"
type="Magento\InventoryDistanceBasedSourceSelection\Plugin\InventoryApi\SourceRepository\Adminhtml\FillSourceLatitudeAndLongitude"/>
</type>
</config>
2 changes: 1 addition & 1 deletion InventoryDistanceBasedSourceSelection/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<type name="Magento\InventoryApi\Api\SourceRepositoryInterface">
<plugin sortOrder="10" name="updateSourceLatitudeAndLongitude"
type="Magento\InventoryDistanceBasedSourceSelection\Plugin\FillSourceLatitudeAndLongitude"/>
type="Magento\InventoryDistanceBasedSourceSelection\Plugin\InventoryApi\SourceRepository\FillSourceLatitudeAndLongitude"/>
</type>

<type name="Magento\Config\Model\Config\TypePool">
Expand Down