Skip to content

Commit 50b36ec

Browse files
committed
minor code improvements and removing code that relies on our closed source geocoding module
1 parent e01c2d8 commit 50b36ec

File tree

2 files changed

+17
-63
lines changed

2 files changed

+17
-63
lines changed

app/code/community/Demac/MultiLocationInventory/Helper/Data.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public function getRegions($countryCode)
4747
/**
4848
* Get latitude and longitude of an address and country.
4949
*
50-
* Uses Demac_Geocoding library if it is available.
51-
*
5250
* @param $postalCode
5351
* @param $country
5452
*
@@ -57,39 +55,30 @@ public function getRegions($countryCode)
5755
* @TODO Move to the location helper.
5856
* @TODO Isolate curl functionality so it is less exposed. (maybe use Zend_Http as part of this)
5957
* @TODO Come up with a better solution to sleep(1), at the very least sleep in microseconds.
60-
* @TODO if we keep sleep related functionality add comments explaining that it is for the google api timeouts.
6158
*/
6259
public function getLatLong($postalCode, $country)
6360
{
61+
//This is used to avoid being rate limited by the google maps api.
6462
sleep(1);
6563
$postalCode = urlencode($postalCode);
6664
$country = urlencode($country);
6765
$url = "http://maps.google.com/maps/api/geocode/json?address=$postalCode&sensor=false&region=$country";
68-
$ch = curl_init();
69-
curl_setopt($ch, CURLOPT_URL, $url);
70-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
71-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
72-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
73-
$response = curl_exec($ch);
74-
curl_close($ch);
75-
$response_a = json_decode($response);
76-
77-
//@TODO handle invalid api responses that don't contain this element.
78-
$lat = $response_a->results[0]->geometry->location->lat;
79-
$long = $response_a->results[0]->geometry->location->lng;
80-
81-
return array($lat, $long);
82-
}
83-
84-
/**
85-
* Checks if the Demac_Geocoding extension is available (recommended).
86-
*
87-
* @returns Boolean
88-
* @TODO Move this to the Location helper.
89-
*/
90-
public function geocodingAvailable()
91-
{
92-
return (bool) Mage::getModel('geocoding/geocode');
66+
try {
67+
$ch = curl_init();
68+
curl_setopt($ch, CURLOPT_URL, $url);
69+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
70+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
71+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
72+
$response = curl_exec($ch);
73+
curl_close($ch);
74+
$response_a = json_decode($response);
75+
$lat = $response_a->results[0]->geometry->location->lat;
76+
$long = $response_a->results[0]->geometry->location->lng;
77+
return array($lat, $long);
78+
} catch (Exception $e) {
79+
//@TODO return false and methods calling this to accept false as a response
80+
return array(0, 0);
81+
}
9382
}
9483

9584

app/code/community/Demac/MultiLocationInventory/Helper/Location.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,6 @@ public function getClosestLocationWithProduct($customerCoordinates, $productId,
3333
return false;
3434
}
3535

36-
/**
37-
* Finds the closest location to a postal code, with an optional list of locations to exclude.
38-
*
39-
* @param array $customerCoordinates The customer's location (array with 2 keys: latitude/longitude).
40-
* @param array $excludes Locations to exclude from the search (e.g. because we know they don't stock an item).
41-
*
42-
* @return bool|Demac_MultiLocationInventory_Model_Location
43-
* @throws Mage_Core_Exception Throws exception when Demac_Geocoding isn't loaded.
44-
*/
45-
protected function getClosestLocation($customerCoordinates, $excludes = array())
46-
{
47-
if(!Mage::helper('geocoding')) {
48-
Mage::throwException('Demac_Geocoding module must be loaded before calling getClosestLocation.');
49-
}
50-
$closestLocation = false;
51-
$closestDistance = 0;
52-
$locations = Mage::getModel('demac_multilocationinventory/location')->getCollection();
53-
foreach ($locations as $location) {
54-
if(!in_array($location->getId(), $excludes)) {
55-
$locationCoordinates = array(
56-
'latitude' => $location->getLat(),
57-
'longitude' => $location->getLong()
58-
);
59-
60-
$locationDistance = Mage::helper('geocoding')->getGeocodingEngine()->findDistance($customerCoordinates, $locationCoordinates);
61-
if($closestLocation == false || $closestDistance > $locationDistance) {
62-
$closestLocation = $location;
63-
$closestDistance = $locationDistance;
64-
}
65-
}
66-
}
67-
68-
return $closestLocation;
69-
}
70-
7136
/**
7237
*
7338
*

0 commit comments

Comments
 (0)