Passed
Push — master ( 95203a...4fa206 )
by MusikAnimal
04:54
created

SimpleEditCounter::getUserId()   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
namespace Xtools;
7
8
use Symfony\Component\DependencyInjection\Container;
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
13
 * the results of the query and return it.
14
 * @codeCoverageIgnore
15
 */
16
class SimpleEditCounter extends Model
17
{
18
    /** @var Container The DI container. */
19
    protected $container;
20
21
    /** @var Project The project. */
22
    protected $project;
23
24
    /** @var User The user. */
25
    protected $user;
26
27
    /** @var string|int Which namespace we are querying for. */
28
    protected $namespace;
29
30
    /** @var false|int Start date as Unix timestamp. */
31
    protected $start;
32
33
    /** @var false|int End date as Unix timestamp. */
34
    protected $end;
35
36
    /** @var array The Simple Edit Counter results. */
37
    protected $data = [
38
        'userId' => null,
39
        'deletedEditCount' => 0,
40
        'liveEditCount' => 0,
41
        'userGroups' => [],
42
        'globalUserGroups' => [],
43
    ];
44
45
    /**
46
     * Constructor for the SimpleEditCounter class.
47
     * @param Container $container The DI container.
48
     * @param Project   $project
49
     * @param User      $user
50
     * @param string    $namespace Namespace ID or 'all'.
51
     * @param false|int $start As Unix timestamp.
52
     * @param false|int $end As Unix timestamp.
53
     */
54
    public function __construct(
55
        Container $container,
56
        Project $project,
57
        User $user,
58
        $namespace = 'all',
59
        $start = false,
60
        $end = false
61
    ) {
62
        $this->container = $container;
63
        $this->project = $project;
64
        $this->user = $user;
65
        $this->namespace = $namespace == '' ? 0 : $namespace;
66
        $this->start = $start;
67
        $this->end = $end;
68
    }
69
70
    /**
71
     * Fetch the data from the database and API,
72
     * then set class properties with the values.
73
     */
74
    public function prepareData()
75
    {
76
        $results = $this->getRepository()->fetchData(
77
            $this->project,
78
            $this->user,
79
            $this->namespace,
80
            $this->start,
81
            $this->end
82
        );
83
84
        // Iterate over the results, putting them in the right variables
85
        foreach ($results as $row) {
86
            switch ($row['source']) {
87
                case 'id':
88
                    $this->data['userId'] = $row['value'];
89
                    break;
90
                case 'arch':
91
                    $this->data['deletedEditCount'] = $row['value'];
92
                    break;
93
                case 'rev':
94
                    $this->data['liveEditCount'] = $row['value'];
95
                    break;
96
                case 'groups':
97
                    $this->data['userGroups'][] = $row['value'];
98
                    break;
99
            }
100
        }
101
102
        if (!$this->container->getParameter('app.single_wiki')) {
103
            $this->data['globalUserGroups'] = $this->user->getGlobalGroups($this->project);
104
        }
105
    }
106
107
    /**
108
     * Get the namespace set on the class instance.
109
     * @return int|string Namespace ID or 'all' for all namespaces.
110
     */
111
    public function getNamespace()
112
    {
113
        return $this->namespace;
114
    }
115
116
    /**
117
     * Get date opening date range.
118
     * @return false|int
119
     */
120
    public function getStart()
121
    {
122
        return $this->start;
123
    }
124
125
    /**
126
     * Get date closing date range.
127
     * @return false|int
128
     */
129
    public function getEnd()
130
    {
131
        return $this->end;
132
    }
133
134
    /**
135
     * Has date range?
136
     * @return bool
137
     */
138
    public function hasDateRange()
139
    {
140
        return $this->start !== false || $this->end !== false;
141
    }
142
143
    /**
144
     * Get back all the data as a single associative array.
145
     * @return array
146
     */
147
    public function getData()
148
    {
149
        return $this->data;
150
    }
151
152
    /**
153
     * Get the user's ID.
154
     * @return int
155
     */
156
    public function getUserId()
157
    {
158
        return $this->data['userId'];
159
    }
160
161
    /**
162
     * Get the number of deleted edits.
163
     * @return int
164
     */
165
    public function getDeletedEditCount()
166
    {
167
        return $this->data['deletedEditCount'];
168
    }
169
170
    /**
171
     * Get the number of live edits.
172
     * @return int
173
     */
174
    public function getLiveEditCount()
175
    {
176
        return $this->data['liveEditCount'];
177
    }
178
179
    /**
180
     * Get the total number of edits.
181
     * @return int
182
     */
183
    public function getTotalEditCount()
184
    {
185
        return $this->data['deletedEditCount'] + $this->data['liveEditCount'];
186
    }
187
188
    /**
189
     * Get the local user groups.
190
     * @return string[]
191
     */
192
    public function getUserGroups()
193
    {
194
        return $this->data['userGroups'];
195
    }
196
197
    /**
198
     * Get the global user groups.
199
     * @return string[]
200
     */
201
    public function getGlobalUserGroups()
202
    {
203
        return $this->data['globalUserGroups'];
204
    }
205
}
206