1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryDistanceBasedSourceSelection\Model\ResourceModel;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryDistanceBasedSourceSelection\Model\Convert\AddressToString;
use Magento\InventorySourceSelectionApi\Api\Data\AddressInterface;
/**
* Get geoname data by postcode
*/
class GetGeoNameDataByAddress
{
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var AddressToString
*/
private $addressToString;
/**
* GetGeoNameDataByPostcode constructor.
*
* @param ResourceConnection $resourceConnection
* @param AddressToString $addressToString
*/
public function __construct(
ResourceConnection $resourceConnection,
AddressToString $addressToString
) {
$this->resourceConnection = $resourceConnection;
$this->addressToString = $addressToString;
}
/**
* Return geonames information using a fallback mechanism
*
* @param AddressInterface $address
* @return array
* @throws NoSuchEntityException
*/
public function execute(AddressInterface $address): array
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('inventory_geoname');
$qry = $connection->select()->from($tableName)
->where('country_code = ?', $address->getCountry())
->where('postcode = ?', $address->getPostcode())
->limit(1);
$row = $connection->fetchRow($qry);
if (!$row) {
$qry = $connection->select()->from($tableName)
->where('country_code = ?', $address->getCountry())
->where('city = ?', $address->getCity())
->limit(1);
$row = $connection->fetchRow($qry);
}
if (!$row) {
$qry = $connection->select()->from($tableName)
->where('country_code = ?', $address->getCountry())
->where('region = ?', $address->getRegion())
->limit(1);
$row = $connection->fetchRow($qry);
}
if (!$row) {
throw new NoSuchEntityException(
__('Unknown geoname for %1', $this->addressToString->execute($address))
);
}
return $row;
}
}