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

CachedRDnsLookupProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 9
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getReverseDNS() 9 29 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
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