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

CachedApiAntispoofProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 26
c 2
b 1
f 1
dl 0
loc 51
ccs 0
cts 34
cp 0
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B getSpoofs() 0 49 8
1
<?php
2
3
/**
4
 * Cached API Antispoof Provider
5
 *
6
 * Provides a list of similar usernames from a MediaWiki API module, and caches
7
 * it in the database.
8
 */
9
class CachedApiAntispoofProvider implements IAntiSpoofProvider
10
{
11
	public function getSpoofs($username)
12
	{
13
		global $mediawikiWebServiceEndpoint;
14
15
		$cacheResult = AntiSpoofCache::getByUsername($username, gGetDb());
16
		if ($cacheResult == false) {
17
			// get the data from the API
18
			$data = file_get_contents($mediawikiWebServiceEndpoint . "?action=antispoof&format=php&username=" . urlencode($username));
19
20
			$cacheEntry = new AntiSpoofCache();
21
			$cacheEntry->setDatabase(gGetDb());
22
			$cacheEntry->setUsername($username);
23
			$cacheEntry->setData($data);
24
			$cacheEntry->save();
25
26
			$cacheResult = $cacheEntry;
27
		}
28
		else {
29
			$data = $cacheResult->getData();
30
		}
31
32
		$result = unserialize($data);
33
34
		if (!isset($result['antispoof']) || !isset($result['antispoof']['result'])) {
35
			$cacheResult->delete();
36
37
			if (isset($result['error']['info'])) {
38
				throw new Exception("Unrecognised API response to query: " . $result['error']['info']);
39
			}
40
41
			throw new Exception("Unrecognised API response to query.");
42
		}
43
44
		if ($result['antispoof']['result'] == "pass") {
45
			// All good here!
46
			return array();
47
		}
48
49
		if ($result['antispoof']['result'] == "conflict") {
50
			// we've got conflicts, let's do something with them.
51
			return $result['antispoof']['users'];
52
		}
53
54
		if ($result['antispoof']['result'] == "error") {
55
			// we've got conflicts, let's do something with them.
56
			throw new Exception("Encountered error while getting result: " . $result['antispoof']['error']);
57
		}
58
59
		throw new Exception("Unrecognised API response to query.");
60
	}
61
}
62