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

CachedRDnsLookupProvider::getRdns()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 8.8571
cc 3
eloc 15
nc 3
nop 1
1
<?php
2
namespace Waca\Providers;
3
4
use Waca\DataObjects\RDnsCache;
5
use Waca\PdoDatabase;
6
use Waca\Providers\Interfaces\IRDnsProvider;
7
8
/**
9
 * Cached rDNS Lookup Provider
10
 *
11
 * Provides a service to look up the reverse DNS of an IP address, and caches
12
 * the result in the database.
13
 */
14
class CachedRDnsLookupProvider implements IRDnsProvider
15
{
16
	private $database;
17
18
	public function __construct(PdoDatabase $database)
19
	{
20
		$this->database = $database;
21
	}
22
23
	public function getReverseDNS($address)
24
	{
25
		$address = trim($address);
26
27
		// lets look in our cache database first.
28
		$rDns = RDnsCache::getByAddress($address, $this->database);
29
30
		if ($rDns instanceof RDnsCache) {
31
			// touch cache timer
32
			$rDns->save();
33
34
			return $rDns->getData();
35
		}
36
37
		// OK, it's not there, let's do an rDNS lookup.
38
		$result = @ gethostbyaddr($address);
39
40 View Code Duplication
		if ($result !== false) {
0 ignored issues
show
Duplication introduced by
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...
41
			$rDns = new RDnsCache();
42
			$rDns->setDatabase($this->database);
43
			$rDns->setAddress($address);
44
			$rDns->setData($result);
45
			$rDns->save();
46
47
			return $result;
48
		}
49
50
		return null;
51
	}
52
}
53