Failed Conditions
Branch newinternal (3df7fe)
by Simon
04:13
created

TorExitProvider::regenerate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 55
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 55
ccs 0
cts 31
cp 0
rs 8.7752
cc 5
eloc 29
nc 5
nop 3
crap 30

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Providers;
10
11
use Waca\Helpers\HttpHelper;
12
use Waca\PdoDatabase;
13
14
class TorExitProvider
15
{
16
	/** @var PdoDatabase */
17
	private $database;
18
19
	/**
20
	 * TorExitProvider constructor.
21
	 *
22
	 * @param PdoDatabase $database
23
	 */
24
	public function __construct(PdoDatabase $database)
25
	{
26
		$this->database = $database;
27
	}
28
29
	/**
30
	 * Checks whether an IP address is a Tor exit node for one of the pre-cached IP addresses.
31
	 *
32
	 * @param string $ip IP Address
33
	 *
34
	 * @return bool
35
	 */
36 View Code Duplication
	public function isTorExit($ip)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
37
	{
38
		$statement = $this->database->prepare('SELECT COUNT(1) FROM tornodecache WHERE ipaddr = :ip');
39
40
		$statement->execute(array(':ip' => $ip));
41
42
		$count = $statement->fetchColumn();
43
		$statement->closeCursor();
44
45
		if ($count > 0) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $count > 0;.
Loading history...
46
			return true;
47
		}
48
		else {
49
			return false;
50
		}
51
	}
52
53
	public static function regenerate(PdoDatabase $database, HttpHelper $httpHelper, $destinationIps)
54
	{
55
		$query = <<<SQL
56
INSERT INTO tornodecache (ipaddr, exitaddr, exitport)
57
VALUES (:ipaddr, :exitaddr, :exitport)
58
ON DUPLICATE KEY
59
UPDATE touched = CURRENT_TIMESTAMP, updateversion = updateversion + 1
60
SQL;
61
62
		$statement = $database->prepare($query);
63
64
		foreach ($destinationIps as $ip) {
65
			echo 'Fetching data for ' . $ip . PHP_EOL;
66
67
			$statement->bindValue(':exitaddr', $ip);
68
69
			$http = $httpHelper->get(
70
				'https://check.torproject.org/cgi-bin/TorBulkExitList.py',
71
				array(
72
					'ip'   => $ip,
73
					'port' => 80,
74
				));
75
76
			$https = $httpHelper->get(
77
				'https://check.torproject.org/cgi-bin/TorBulkExitList.py',
78
				array(
79
					'ip'   => $ip,
80
					'port' => 443,
81
				));
82
83
			foreach (array(80 => $http, 443 => $https) as $port => $response) {
84
				echo '  Running for port ' . $ip . ':' . $port . PHP_EOL;
85
86
				$statement->bindValue(':exitport', $port);
87
88
				$lines = explode("\n", $response);
89
90
				foreach ($lines as $line) {
91
					// line contains a comment char, just skip the line.
92
					// This is OK as of 2016-04-06  --stw
93
					if (strpos($line, '#') !== false) {
94
						continue;
95
					}
96
97
					$statement->bindValue(':ipaddr', $line);
98
					$statement->execute();
99
				}
100
			}
101
102
			echo 'Done for ' . $ip . PHP_EOL;
103
		}
104
105
		// kill old cached entries
106
		$database->exec('DELETE FROM tornodecache WHERE touched < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)');
107
	}
108
}