'Unknown error', self::MSG_ZERO_RESULTS => 'The geographical coordinates can\'t be determined for the specified address criteria' ]; /** @var string */ protected $_error; /** @var Zend_Cache_Core */ protected $_cache; /** * @param string $address * @throws Exception */ public function __construct($address) { if (empty($address)) { throw new Exception('Address is empty'); } $this->_address = $address; } public function send() { $cacheId = md5($this->_address); $results = $this->_getCache()->load($cacheId); if (empty($results)) { $params = [ 'address' => $this->_address, 'sensor' => $this->_sensor, 'key' => $this->_getConfig('apiKey'), ]; $query = http_build_query($params); $url = $this->_getConfig('gateway'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url . '?' . $query); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); $json = curl_exec($ch); curl_close($ch); $response = []; if ($json) { $response = json_decode($json, true); } if ($response) { switch (Qs_Array::get($response, 'status', '')) { case self::STATUS_OK: $results = reset($response['results']); $this->_getCache()->save($results, $cacheId); break; case self::STATUS_ZERO_RESULTS: $this->setError($this->_messages[self::MSG_ZERO_RESULTS]); break; default: $this->setError($this->_messages[self::MSG_UNKNOWN_ERROR] . ': ' . Qs_Array::get($response, 'error_message', '')); } } else { $this->setError($this->_messages[self::MSG_UNKNOWN_ERROR] . ': ' . Qs_Array::get($response, 'error_message', '')); } } if (!empty($results)) { $this->_response = new Response($results); return true; } return false; } public function getResponse() { return $this->_response; } protected function _getCache() { if (null === $this->_cache) { $frontendOptions = array( 'lifetime' => null, 'automatic_serialization' => true ); $this->_cache = Qs_Cache::factory('Core', 'File', $frontendOptions); } return $this->_cache; } protected function _getConfig($field = null) { if (null === $this->_config) { $this->_config = Qs_Config::get($this->_configAlias, Qs_Config::APP_TYPE)->toArray(); } return $field ? $this->_config[$field] : $this->_config; } /** * @param string $error * @return $this */ public function setError($error) { $this->_error = $error; return $this; } /** * @return string */ public function getError() { return $this->_error; } }