Completed
Push — add-pleio-mod ( 6d68a5 )
by
unknown
28:07
created

Helpers   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 143
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 11
loc 143
rs 10
c 0
b 0
f 0
ccs 0
cts 107
cp 0
wmc 20
lcom 1
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
B generateUsername() 11 22 4
A removeUser() 0 17 3
A removeAllRelationships() 0 6 1
A removeAllMetadata() 0 6 1
A removeAllPrivateSettings() 0 6 1
B anonymizeAccount() 0 36 2
A getSiteEmail() 0 10 2
A signData() 0 5 1
A loadSignedData() 0 20 4
A emailInWhitelist() 0 4 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
namespace ModPleio;
3
4
class Helpers {
5
    public static function generateUsername($username) {
6
        $hidden = access_show_hidden_entities(true);
7
8
        while (strlen($username) < 4) {
9
            $username .= "0";
10
        }
11
12 View Code Duplication
        if (get_user_by_username($username)) {
13
            $i = 1;
14
15
            while (get_user_by_username($username . $i)) {
16
                $i++;
17
            }
18
19
            $result = $username . $i;
20
        } else {
21
            $result = $username;
22
        }
23
24
        access_show_hidden_entities($hidden);
25
        return $result;
26
    }
27
28
    public static function removeUser(\ElggUser $user) {
29
        if (!$user || !$user instanceof \ElggUser) {
30
            return false;
31
        }
32
33
        // make sure the user cannot login any more, also not with the "rememberme" cookie
34
        $user->ban("banned");
35
        $user->removeAdmin();
36
        $user->save();
37
38
        $result = Helpers::removeAllRelationships($user);
39
        $result &= Helpers::removeAllMetadata($user);
40
        $result &= Helpers::removeAllPrivateSettings($user);
41
        $result &= Helpers::anonymizeAccount($user);
42
43
        return $result;
44
    }
45
46
    public function removeAllRelationships(\ElggUser $user) {
47
        $dbprefix = elgg_get_config("dbprefix");
48
        $result = delete_data("DELETE FROM {$dbprefix}entity_relationships WHERE guid_one = {$user->guid} OR guid_two = {$user->guid}");
49
50
        return ($result !== false);
51
    }
52
53
    public function removeAllMetadata(\ElggUser $user) {
54
        $dbprefix = elgg_get_config("dbprefix");
55
        $result = delete_data("DELETE FROM {$dbprefix}metadata WHERE entity_guid = {$user->guid}");
56
57
        return ($result !== false);
58
    }
59
60
    public function removeAllPrivateSettings(\ElggUser $user) {
61
        $dbprefix = elgg_get_config("dbprefix");
62
        $result = delete_data("DELETE FROM {$dbprefix}private_settings WHERE entity_guid = {$user->guid}");
63
64
        return ($result !== false);
65
    }
66
67
    public static function anonymizeAccount(\ElggUser $user) {
68
        $dbprefix = elgg_get_config("dbprefix");
69
70
        // set site_guid to 0 as we do not want to display the entity in the listing any more
71
        $result = update_data("UPDATE {$dbprefix}entities SET
72
            site_guid = 0
73
            WHERE guid = {$user->guid}"
74
        );
75
76
        $result &= update_data("UPDATE {$dbprefix}users_entity SET 
77
            password = NULL,
78
            salt = NULL,
79
            password_hash = NULL,
80
            name = 'Verwijderde gebruiker',
81
            username = 'verwijderd{$user->guid}',
82
            email = NULL,
83
            pleio_guid = NULL,
84
            language = NULL,
85
            last_action = 0,
86
            prev_last_action = 0,
87
            last_login = 0,
88
            prev_last_login = 0
89
            WHERE guid = {$user->guid}"
90
        );
91
92
        _elgg_invalidate_cache_for_entity($user->guid);
93
94
        if (is_memcache_available()) {
95
            $newentity_cache = new \ElggMemcache("new_entity_cache");
96
            $newentity_cache->delete($user->guid);
97
        }
98
99
        elgg_trigger_event("update", "user", get_entity($user->guid));
0 ignored issues
show
Documentation introduced by
get_entity($user->guid) is of type object<ElggEntity>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
101
        return ($result !== false);
102
    }
103
104
    public static function getSiteEmail() {
105
        global $CONFIG;
106
107
        $site = elgg_get_site_entity();
108
        if ($site->email) {
109
            return $site->email;
110
        } else {
111
            return "noreply@" . get_site_domain($CONFIG->site_guid);
112
        }
113
    }
114
115
    public static function signData($data) {
116
        $data = base64_encode(json_encode($data)) . ":" . time();
117
        $hash = hash_hmac("sha256", $data, get_site_secret());
118
        return "{$hash}:{$data}";
119
    }
120
121
    public static function loadSignedData($data) {
122
        list($input_hash, $data, $timestamp) = explode(":", $data);
123
        $verification_hash = hash_hmac("sha256", $data . ":" . $timestamp, get_site_secret());
124
125
        if ($verification_hash !== $input_hash) {
126
            return false;
127
        }
128
129
        if (time() > $timestamp + 3600*24) {
130
            return false;
131
        }
132
133
        $data = json_decode(base64_decode($data), true);
134
135
        if (!$data) {
136
            return false;
137
        }
138
139
        return $data;
140
    }
141
142
    public static function emailInWhitelist($email) {
143
        $domain = pleio_get_domain_from_email($email);
144
        return pleio_domain_in_whitelist($domain);
145
    }
146
}