Completed
Push — rbac ( 1ec5d5...5c33ef )
by Simon
04:39
created

BlacklistHelper::performWikiLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

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