CachedApiAntispoofProvider::getSpoofs()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 56
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 56
rs 8.4444
c 0
b 0
f 0
cc 8
nc 12
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Providers;
11
12
use Exception;
13
use Waca\DataObjects\AntiSpoofCache;
14
use Waca\DataObjects\Domain;
15
use Waca\Helpers\HttpHelper;
16
use Waca\PdoDatabase;
17
use Waca\Providers\Interfaces\IAntiSpoofProvider;
18
19
/**
20
 * Cached API Antispoof Provider
21
 *
22
 * Provides a list of similar usernames from a MediaWiki API module, and caches
23
 * it in the database.
24
 */
25
class CachedApiAntispoofProvider implements IAntiSpoofProvider
26
{
27
    /**
28
     * @var PdoDatabase
29
     */
30
    private $database;
31
32
    /**
33
     * @var HttpHelper
34
     */
35
    private $httpHelper;
36
37
    public function __construct(PdoDatabase $database, HttpHelper $httpHelper)
38
    {
39
        $this->database = $database;
40
        $this->httpHelper = $httpHelper;
41
    }
42
43
    public function getSpoofs($username)
44
    {
45
        // FIXME: domains!
46
        /** @var Domain $domain */
47
        $domain = Domain::getById(1, $this->database);
48
49
        /** @var AntiSpoofCache $cacheResult */
50
        $cacheResult = AntiSpoofCache::getByUsername($username, $this->database);
51
        if ($cacheResult == false) {
0 ignored issues
show
introduced by
The condition $cacheResult == false is always false.
Loading history...
52
            // get the data from the API
53
            $data = $this->httpHelper->get($domain->getWikiApiPath(), array(
54
                'action'   => 'antispoof',
55
                'format'   => 'php',
56
                'username' => $username,
57
            ));
58
59
            $cacheEntry = new AntiSpoofCache();
60
            $cacheEntry->setDatabase($this->database);
61
            $cacheEntry->setUsername($username);
62
            $cacheEntry->setData($data);
63
            $cacheEntry->save();
64
65
            $cacheResult = $cacheEntry;
66
        }
67
        else {
68
            $data = $cacheResult->getData();
69
        }
70
71
        $result = unserialize($data);
72
73
        if (!isset($result['antispoof']) || !isset($result['antispoof']['result'])) {
74
            $cacheResult->delete();
75
76
            if (isset($result['error']['info'])) {
77
                throw new Exception("Unrecognised API response to query: " . $result['error']['info']);
78
            }
79
80
            throw new Exception("Unrecognised API response to query.");
81
        }
82
83
        if ($result['antispoof']['result'] == "pass") {
84
            // All good here!
85
            return array();
86
        }
87
88
        if ($result['antispoof']['result'] == "conflict") {
89
            // we've got conflicts, let's do something with them.
90
            return $result['antispoof']['users'];
91
        }
92
93
        if ($result['antispoof']['result'] == "error") {
94
            // we've got conflicts, let's do something with them.
95
            throw new Exception("Encountered error while getting result: " . $result['antispoof']['error']);
96
        }
97
98
        throw new Exception("Unrecognised API response to query.");
99
    }
100
}
101