Completed
Pull Request — master (#526)
by Michael
16:45 queued 06:57
created

CachedApiAntispoofProvider::getSpoofs()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 8
eloc 25
c 2
b 1
f 1
nc 12
nop 1
dl 0
loc 49
ccs 0
cts 34
cp 0
crap 72
rs 8.4444
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