Failed Conditions
Branch newinternal (104de7)
by Simon
09:33
created

includes/Providers/IpLocationProvider.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Waca\Providers;
3
4
use Exception;
5
use SimpleXMLElement;
6
use Waca\DataObjects\GeoLocation;
7
use Waca\PdoDatabase;
8
use Waca\Providers\Interfaces\ILocationProvider;
9
10
/**
11
 * IP location provider
12
 */
13
class IpLocationProvider implements ILocationProvider
14
{
15
	private $apikey;
16
	private $database;
17
18
	public function __construct(PdoDatabase $database, $apikey)
19
	{
20
		$this->database = $database;
21
		$this->apikey = $apikey;
22
	}
23
24
	public function getIpLocation($address)
25
	{
26
		$address = trim($address);
27
28
		// lets look in our database first.
29
		$location = GeoLocation::getByAddress($address, $this->database);
30
31
		if ($location != null) {
32
			// touch cache timer
33
			$location->save();
34
35
			return $location->getData();
36
		}
37
38
		// OK, it's not there, let's do an IP2Location lookup.
39
		$result = $this->getResult($address);
40
41 View Code Duplication
		if ($result != null) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
			$location = new GeoLocation();
43
			$location->setDatabase($this->database);
44
			$location->setAddress($address);
45
			$location->setData($result);
46
			$location->save();
47
48
			return $result;
49
		}
50
51
		return null;
52
	}
53
54
	// adapted from http://www.ipinfodb.com/ip_location_api.php
55
56
	/**
57
	 * @param string $ip
58
	 *
59
	 * @return array|null
60
	 */
61
	private function getResult($ip)
62
	{
63
		try {
64
			if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
65
				$xml = @file_get_contents($this->getApiBase() . '?key=' . $this->apikey . '&ip=' . $ip . '&format=xml');
66
67
				$response = @new SimpleXMLElement($xml);
68
69
				$result = array();
70
71
				foreach ($response as $field => $value) {
72
					$result[(string)$field] = (string)$value;
73
				}
74
75
				return $result;
76
			}
77
		}
78
		catch (Exception $ex) {
79
			return null;
80
81
			// TODO: do something smart here, or wherever we use this value.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
82
			// This is just a temp hack to squash errors on the UI for now.
83
		}
84
85
		return null;
86
	}
87
88
	protected function getApiBase()
89
	{
90
		return "http://api.ipinfodb.com/v3/ip-city/";
91
	}
92
}
93