Completed
Pull Request — master (#526)
by Michael
01:57
created

CachedRDnsLookupProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 74.36 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 29
loc 39
rs 10
c 0
b 0
f 0
ccs 0
cts 14
cp 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getRdns() 0 28 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Cached RDNS Lookup Provider
5
 *
6
 * Provides a service to look up the reverse DNS of an IP address, and caches
7
 * the result in the database.
8
 */
9
class CachedRDnsLookupProvider implements IRDnsProvider
10
{
11
	private $database;
12
13
	public function __construct(PdoDatabase $database)
14
	{
15
		$this->database = $database;
16
	}
17
18
	public function getRdns($address)
19
	{
20
		$address = trim($address);
21
22
		// lets look in our cache database first.
23
		$rDns = RDnsCache::getByAddress($address, $this->database);
24
25
		if ($rDns != null) {
26
			// touch cache timer
27
			$rDns->save();
28
29
			return $rDns->getData();
30
		}
31
32
		// OK, it's not there, let's do an rdns lookup.
33
		$result = @ gethostbyaddr($address);
34
35
		if ($result !== false) {
36
			$rDns = new RDnsCache();
37
			$rDns->setDatabase($this->database);
38
			$rDns->setAddress($address);
39
			$rDns->setData($result);
40
			$rDns->save();
41
42
			return $result;
43
		}
44
45
		return null;
46
	}
47
}
48