Completed
Pull Request — newinternal (#285)
by Simon
07:17 queued 04:17
created

BlacklistHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 21.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 19
loc 90
ccs 0
cts 43
cp 0
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B isBlacklisted() 19 31 5
A performWikiLookup() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(HttpHelper $httpHelper, $mediawikiWebServiceEndpoint)
33
    {
34
        $this->httpHelper = $httpHelper;
35
        $this->mediawikiWebServiceEndpoint = $mediawikiWebServiceEndpoint;
36
    }
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
    public function isBlacklisted($username)
46
    {
47 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
            $result = $this->cache[$username];
49
            if ($result === false) {
50
                return false;
51
            }
52
53
            return $result['line'];
54
        }
55
56
        try {
57
            $result = $this->performWikiLookup($username);
58
        }
59
        catch (CurlException $ex) {
60
            // LOGME log this, but fail gracefully.
61
            return false;
62
        }
63
64 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
            $this->cache[$username] = false;
67
68
            return false;
69
        }
70
        else {
71
            $this->cache[$username] = $result;
72
73
            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
    private function performWikiLookup($username)
86
    {
87
        $endpoint = $this->mediawikiWebServiceEndpoint;
88
89
        $parameters = array(
90
            'action'       => 'titleblacklist',
91
            'format'       => 'php',
92
            'tbtitle'      => $username,
93
            'tbaction'     => 'new-account',
94
            'tbnooverride' => true,
95
        );
96
97
        $apiResult = $this->httpHelper->get($endpoint, $parameters);
98
99
        $data = unserialize($apiResult);
100
101
        return $data['titleblacklist'];
102
    }
103
}