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

BlacklistHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Waca\Helpers;
4
5
use Waca\Exceptions\CurlException;
6
use Waca\Helpers\Interfaces\IBlacklistHelper;
7
8
class BlacklistHelper implements IBlacklistHelper
9
{
10
	/** @var HttpHelper */
11
	private $httpHelper;
12
	/**
13
	 * Cache of previously requested usernames
14
	 * @var array
15
	 */
16
	private $cache = array();
17
	/** @var string */
18
	private $mediawikiWebServiceEndpoint;
19
20
	/**
21
	 * BlacklistHelper constructor.
22
	 *
23
	 * @param HttpHelper $httpHelper
24
	 * @param string     $mediawikiWebServiceEndpoint
25
	 */
26
	public function __construct(HttpHelper $httpHelper, $mediawikiWebServiceEndpoint)
27
	{
28
		$this->httpHelper = $httpHelper;
29
		$this->mediawikiWebServiceEndpoint = $mediawikiWebServiceEndpoint;
30
	}
31
32
	/**
33
	 * Returns a value indicating whether the provided username is blacklisted by the on-wiki title blacklist
34
	 *
35
	 * @param string $username
36
	 *
37
	 * @return false|string False if the username is not blacklisted, else the blacklist entry.
38
	 */
39
	public function isBlacklisted($username)
40
	{
41 View Code Duplication
		if (isset($this->cache[$username])) {
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...
42
			$result = $this->cache[$username];
43
			if ($result === false) {
44
				return false;
45
			}
46
47
			return $result['line'];
48
		}
49
50
		try {
51
			$result = $this->performWikiLookup($username);
52
		}
53
		catch (CurlException $ex) {
54
			// @todo log this, but fail gracefully.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
55
			return false;
56
		}
57
58 View Code Duplication
		if ($result['result'] === 'ok') {
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...
59
			// not blacklisted
60
			$this->cache[$username] = false;
61
62
			return false;
63
		}
64
		else {
65
			$this->cache[$username] = $result;
66
67
			return $result['line'];
68
		}
69
	}
70
71
	/**
72
	 * Performs a fetch to MediaWiki for the relevant title blacklist entry
73
	 *
74
	 * @param string $username The username to look up
75
	 *
76
	 * @return array
77
	 * @throws CurlException
78
	 */
79
	private function performWikiLookup($username)
80
	{
81
		$endpoint = $this->mediawikiWebServiceEndpoint;
82
83
		$parameters = array(
84
			'action'       => 'titleblacklist',
85
			'format'       => 'php',
86
			'tbtitle'      => $username,
87
			'tbaction'     => 'new-account',
88
			'tbnooverride' => true,
89
		);
90
91
		$apiResult = $this->httpHelper->get($endpoint, $parameters);
92
93
		$data = unserialize($apiResult);
94
95
		return $data['titleblacklist'];
96
	}
97
}