SimpleEditCounter::getUserGroups()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace App\Model;
6
7
use App\Repository\SimpleEditCounterRepository;
8
9
/**
10
 * A SimpleEditCounter provides basic edit count stats about a user.
11
 * This class is too 'simple' to bother with tests, we just get the results of the query and return them.
12
 * @codeCoverageIgnore
13
 */
14
class SimpleEditCounter extends Model
15
{
16
    /** @var bool Whether only limited results are given (due to high edit count). */
17
    private bool $limited = false;
18
19
    /** @var array The Simple Edit Counter results. */
20
    protected array $data = [
21
        'user_id' => null,
22
        'deleted_edit_count' => 0,
23
        'live_edit_count' => 0,
24
        'user_groups' => [],
25
        'global_user_groups' => [],
26
    ];
27
28
    /**
29
     * Constructor for the SimpleEditCounter class.
30
     * @param Project $project
31
     * @param User $user
32
     * @param string|int|null $namespace Namespace ID or 'all'.
33
     * @param false|int $start As Unix timestamp.
34
     * @param false|int $end As Unix timestamp.
35
     */
36
    public function __construct(
37
        SimpleEditCounterRepository $repository,
38
        Project $project,
39
        User $user,
40
        $namespace = 'all',
41
        $start = false,
42
        $end = false
43
    ) {
44
        $this->repository = $repository;
45
        $this->project = $project;
46
        $this->user = $user;
47
48
        if ($this->user->getEditCount($this->project) > $this->user->maxEdits()) {
49
            $this->limited = true;
50
            $this->namespace = 'all';
51
            $this->start = false;
52
            $this->end = false;
53
        } else {
54
            $this->namespace = '' == $namespace ? 0 : $namespace;
55
            $this->start = $start;
56
            $this->end = $end;
57
        }
58
    }
59
60
    /**
61
     * Fetch the data from the database and API,
62
     * then set class properties with the values.
63
     */
64
    public function prepareData(): void
65
    {
66
        if ($this->limited) {
67
            $this->data = [
68
                'user_id' => $this->user->getId($this->project),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                'user_id' => $this->user->/** @scrutinizer ignore-call */ getId($this->project),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
                'total_edit_count' => $this->user->getEditCount($this->project),
70
                'user_groups' => $this->user->getUserRights($this->project),
71
                'approximate' => true,
72
                'namespace' => 'all',
73
            ];
74
        } else {
75
            $this->prepareFullData();
76
        }
77
78
        if (!$this->user->isAnon()) {
79
            $this->data['global_user_groups'] = $this->user->getGlobalUserRights($this->project);
80
        }
81
    }
82
83
    private function prepareFullData(): void
84
    {
85
        $results = $this->repository->fetchData(
0 ignored issues
show
Bug introduced by
The method fetchData() does not exist on App\Repository\Repository. It seems like you code against a sub-type of App\Repository\Repository such as App\Repository\AdminScoreRepository or App\Repository\SimpleEditCounterRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        /** @scrutinizer ignore-call */ 
86
        $results = $this->repository->fetchData(
Loading history...
86
            $this->project,
87
            $this->user,
88
            $this->namespace,
89
            $this->start,
90
            $this->end
91
        );
92
93
        // Iterate over the results, putting them in the right variables
94
        foreach ($results as $row) {
95
            switch ($row['source']) {
96
                case 'id':
97
                    $this->data['user_id'] = (int)$row['value'];
98
                    break;
99
                case 'arch':
100
                    $this->data['deleted_edit_count'] = (int)$row['value'];
101
                    break;
102
                case 'rev':
103
                    $this->data['live_edit_count'] = (int)$row['value'];
104
                    break;
105
                case 'groups':
106
                    $this->data['user_groups'][] = $row['value'];
107
                    break;
108
            }
109
        }
110
    }
111
112
    /**
113
     * Get back all the data as a single associative array.
114
     * @return array
115
     */
116
    public function getData(): array
117
    {
118
        return $this->data;
119
    }
120
121
    /**
122
     * Get the user's ID.
123
     * @return int
124
     */
125
    public function getUserId(): int
126
    {
127
        return $this->data['user_id'];
128
    }
129
130
    /**
131
     * Get the number of deleted edits.
132
     * @return int
133
     */
134
    public function getDeletedEditCount(): int
135
    {
136
        return $this->data['deleted_edit_count'];
137
    }
138
139
    /**
140
     * Get the number of live edits.
141
     * @return int
142
     */
143
    public function getLiveEditCount(): int
144
    {
145
        return $this->data['live_edit_count'];
146
    }
147
148
    /**
149
     * Get the total number of edits.
150
     * @return int
151
     */
152
    public function getTotalEditCount(): int
153
    {
154
        return $this->data['total_edit_count'] ?? $this->data['deleted_edit_count'] + $this->data['live_edit_count'];
155
    }
156
157
    /**
158
     * Get the local user groups.
159
     * @return string[]
160
     */
161
    public function getUserGroups(): array
162
    {
163
        return $this->data['user_groups'];
164
    }
165
166
    /**
167
     * Get the global user groups.
168
     * @return string[]
169
     */
170
    public function getGlobalUserGroups(): array
171
    {
172
        return $this->data['global_user_groups'];
173
    }
174
175
    /**
176
     * Whether or not only limited, approximate data is provided.
177
     * @return bool
178
     */
179
    public function isLimited(): bool
180
    {
181
        return $this->limited;
182
    }
183
}
184