Browse Source

Lib/GeocodeLib updates

* comment incorrect, pause time in microseconds
* option for number of `repeats` vs. hardcoding at 5
* new property for `reachedQueryLimit` which disables all future `geocode`/`reverseGeocode` calls in this run
* `reset` will clear `reachedQueryLimit`
alan bount 11 years ago
parent
commit
de9bb258b3
2 changed files with 31 additions and 3 deletions
  1. 16 3
      Lib/GeocodeLib.php
  2. 15 0
      Test/Case/Lib/GeocodeLibTest.php

+ 16 - 3
Lib/GeocodeLib.php

@@ -61,7 +61,8 @@ class GeocodeLib {
 	 */
 	public $options = array(
 		'log' => false,
-		'pause' => 10000, # in ms
+		'pause' => 10000, # in microseconds (10000 = 0.01 seconds)
+		'repeats' => 5, # if over limits, how many times to repeat
 		'min_accuracy' => self::ACC_COUNTRY,
 		'allow_inconclusive' => true,
 		'expect' => array(), # see accuracyTypes for details
@@ -81,6 +82,7 @@ class GeocodeLib {
 		//'key' => '' # not necessary anymore
 	);
 
+	public $reachedQueryLimit = false;
 	protected $error = array();
 	protected $debug = array();
 
@@ -167,6 +169,7 @@ class GeocodeLib {
 	public function reset($full = true) {
 		$this->error = array();
 		$this->result = null;
+		$this->reachedQueryLimit = false;
 		if (empty($full)) {
 			return;
 		}
@@ -236,6 +239,10 @@ class GeocodeLib {
 	 * @return bool Success
 	 */
 	public function geocode($address, $params = array()) {
+		if ($this->reachedQueryLimit) {
+			$this->setError('Over Query Limit - abort');
+			return false;
+		}
 		$this->reset(false);
 		$this->_setDebug('geocode', compact('address', 'params'));
 		$params = array('address' => $address) + $params;
@@ -298,11 +305,12 @@ class GeocodeLib {
 				return false; # for now...
 			}
 
-			if ($count > 5) {
+			if ($count > $this->options['repeats']) {
 				if ($this->options['log']) {
 					CakeLog::write('geocode', __('Aborted after too many trials with \'%s\'', $address));
 				}
 				$this->setError('Too many trials - abort');
+				$this->reachedQueryLimit = true;
 				return false;
 			}
 			$this->pause(true);
@@ -320,6 +328,10 @@ class GeocodeLib {
 	 * @return bool Success
 	 */
 	public function reverseGeocode($lat, $lng, $params = array()) {
+		if ($this->reachedQueryLimit) {
+			$this->setError('Over Query Limit - abort');
+			return false;
+		}
 		$this->reset(false);
 		$this->_setDebug('reverseGeocode', compact('lat', 'lng', 'params'));
 		$latlng = $lat . ',' . $lng;
@@ -373,11 +385,12 @@ class GeocodeLib {
 				}
 				return false; # for now...
 			}
-			if ($count > 5) {
+			if ($count > $this->options['repeats']) {
 				if ($this->options['log']) {
 					CakeLog::write('geocode', __('Aborted after too many trials with \'%s\'', $latlng));
 				}
 				$this->setError(__('Too many trials - abort'));
+				$this->reachedQueryLimit = true;
 				return false;
 			}
 			$this->pause(true);

+ 15 - 0
Test/Case/Lib/GeocodeLibTest.php

@@ -324,6 +324,21 @@ class GeocodeLibTest extends MyCakeTestCase {
 	}
 
 	/**
+	 * GeocodeLibTest::testGeocodeReachedQueryLimit()
+	 *
+	 * @return void
+	 */
+	public function testGeocodeReachedQueryLimit() {
+		$this->Geocode->reachedQueryLimit = true;
+		$address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
+		$result = $this->Geocode->geocode($address);
+		$this->assertFalse($result);
+
+		$result = $this->Geocode->error();
+		$this->assertEquals('Over Query Limit - abort', $result);
+	}
+
+	/**
 	 * GeocodeLibTest::testGeocodeBadApiKey()
 	 *
 	 * @return void