geositemap.php
1.77 KB
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
<?php
// namespace components\com_jmap\models;
/**
* @package JMAP::SITEMAP::components::com_jmap
* @subpackage models
* @author Joomla! Extensions Store
* @copyright (C) 2015 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined ( '_JEXEC' ) or die ( 'Restricted access' );
/**
* Main sitemap model public responsibilities interface
*
* @package JMAP::SITEMAP::components::com_jmap
* @subpackage models
* @since 3.5
*/
interface IJMapModelSitemap {
/**
* Get the Data
* @access public
* @param Object $httpClient
* @return Object
*/
public function getSitemapData(JMapHttp $httpClient);
}
/**
* Main sitemap model class <<testable_behavior>>
*
* @package JMAP::SITEMAP::components::com_jmap
* @subpackage models
* @since 3.5
*/
class JMapModelGeositemap extends JMapModel implements IJMapModelSitemap {
/**
* Get the Data
* @access public
* @param Object $httpClient
* @return Object
*/
public function getSitemapData(JMapHttp $httpClient) {
// Try to get informations
try {
$address = $this->getComponentParams()->get('geositemap_address', null);
if(!$address) {
throw new Exception(null);
}
$encodedAddress = urlencode($address);
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $encodedAddress . '&key=AIzaSyDNlp3wN1Al_ksW92rmb5Y96RQGn68tKb8';
$response = $httpClient->get($url)->body;
if($response) {
$decodedUpdateInfos = json_decode($response);
}
if(!is_object($decodedUpdateInfos) || !is_array($decodedUpdateInfos->results) || $decodedUpdateInfos->status != 'OK') {
throw new Exception(null);
}
return $decodedUpdateInfos->results[0];
} catch(JMapException $e) {
return false;
} catch(Exception $e) {
return false;
}
}
}