Passed
Push — master ( 75dacc...adc049 )
by MusikAnimal
05:30
created

SimpleEditCounter::getLiveEditCount()   A

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
 * This file contains only the SimpleEditCounter class.
4
 */
5
6
declare(strict_types = 1);
7
8
namespace AppBundle\Model;
9
10
/**
11
 * A SimpleEditCounter provides basic edit count stats about a user.
12
 * This class is too 'simple' to bother with tests, we just get the results of the query and return them.
13
 * @codeCoverageIgnore
14
 */
15
class SimpleEditCounter extends Model
16
{
17
    /** @var bool Whether only limited results are given (due to high edit count). */
18
    private $limited = false;
19
20
    /** @var array The Simple Edit Counter results. */
21
    protected $data = [
22
        'userId' => null,
23
        'deletedEditCount' => 0,
24
        'liveEditCount' => 0,
25
        'userGroups' => [],
26
        'globalUserGroups' => [],
27
    ];
28
29
    /** @var int If the user has more than this many edits, only the approximate system edit count will be used. */
30
    public const MAX_EDIT_COUNT = 500000;
31
32
    /**
33
     * Constructor for the SimpleEditCounter class.
34
     * @param Project $project
35
     * @param User $user
36
     * @param string $namespace Namespace ID or 'all'.
37
     * @param false|int $start As Unix timestamp.
38
     * @param false|int $end As Unix timestamp.
39
     */
40
    public function __construct(Project $project, User $user, string $namespace = 'all', $start = false, $end = false)
41
    {
42
        $this->project = $project;
43
        $this->user = $user;
44
45
        if ($this->user->getEditCount($this->project) > self::MAX_EDIT_COUNT) {
46
            $this->limited = true;
47
            $this->namespace = 'all';
48
            $this->start = false;
49
            $this->end = false;
50
        } else {
51
            $this->namespace = '' == $namespace ? 0 : $namespace;
52
            $this->start = $start;
53
            $this->end = $end;
54
        }
55
    }
56
57
    /**
58
     * Fetch the data from the database and API,
59
     * then set class properties with the values.
60
     */
61
    public function prepareData(): void
62
    {
63
        if ($this->limited) {
64
            $this->data = [
65
                'userId' => $this->user->getId($this->project),
66
                'totalEditCount' => $this->user->getEditCount($this->project),
67
                'userGroups' => $this->user->getUserRights($this->project),
68
                'approximate' => true,
69
                'namespace' => 'all',
70
            ];
71
        } else {
72
            $this->prepareFullData();
73
        }
74
75
        if (!$this->user->isAnon()) {
76
            $this->data['globalUserGroups'] = $this->user->getGlobalUserRights($this->project);
77
        }
78
    }
79
80
    private function prepareFullData(): void
81
    {
82
        $results = $this->getRepository()->fetchData(
0 ignored issues
show
Bug introduced by
The method fetchData() does not exist on AppBundle\Repository\Repository. It seems like you code against a sub-type of AppBundle\Repository\Repository such as AppBundle\Repository\AdminScoreRepository or AppBundle\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

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