Passed
Push — master ( 95d2e4...8875f5 )
by MusikAnimal
05:41
created

SimpleEditCounter::getNamespace()   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 array The Simple Edit Counter results. */
22
    protected $data = [
23
        'userId' => null,
24
        'deletedEditCount' => 0,
25
        'liveEditCount' => 0,
26
        'userGroups' => [],
27
        'globalUserGroups' => [],
28
    ];
29
30
    /**
31
     * Constructor for the SimpleEditCounter class.
32
     * @param Container $container The DI container.
33
     * @param Project   $project
34
     * @param User      $user
35
     * @param string    $namespace Namespace ID or 'all'.
36
     * @param false|int $start As Unix timestamp.
37
     * @param false|int $end As Unix timestamp.
38
     */
39
    public function __construct(
40
        Container $container,
41
        Project $project,
42
        User $user,
43
        $namespace = 'all',
44
        $start = false,
45
        $end = false
46
    ) {
47
        $this->container = $container;
48
        $this->project = $project;
49
        $this->user = $user;
50
        $this->namespace = $namespace == '' ? 0 : $namespace;
51
        $this->start = $start;
52
        $this->end = $end;
53
    }
54
55
    /**
56
     * Fetch the data from the database and API,
57
     * then set class properties with the values.
58
     */
59
    public function prepareData()
60
    {
61
        $results = $this->getRepository()->fetchData(
62
            $this->project,
63
            $this->user,
64
            $this->namespace,
65
            $this->start,
66
            $this->end
67
        );
68
69
        // Iterate over the results, putting them in the right variables
70
        foreach ($results as $row) {
71
            switch ($row['source']) {
72
                case 'id':
73
                    $this->data['userId'] = $row['value'];
74
                    break;
75
                case 'arch':
76
                    $this->data['deletedEditCount'] = $row['value'];
77
                    break;
78
                case 'rev':
79
                    $this->data['liveEditCount'] = $row['value'];
80
                    break;
81
                case 'groups':
82
                    $this->data['userGroups'][] = $row['value'];
83
                    break;
84
            }
85
        }
86
87
        if (!$this->user->isAnon() && !$this->container->getParameter('app.single_wiki')) {
88
            $this->data['globalUserGroups'] = $this->user->getGlobalUserRights($this->project);
89
        }
90
    }
91
92
    /**
93
     * Get back all the data as a single associative array.
94
     * @return array
95
     */
96
    public function getData()
97
    {
98
        return $this->data;
99
    }
100
101
    /**
102
     * Get the user's ID.
103
     * @return int
104
     */
105
    public function getUserId()
106
    {
107
        return $this->data['userId'];
108
    }
109
110
    /**
111
     * Get the number of deleted edits.
112
     * @return int
113
     */
114
    public function getDeletedEditCount()
115
    {
116
        return $this->data['deletedEditCount'];
117
    }
118
119
    /**
120
     * Get the number of live edits.
121
     * @return int
122
     */
123
    public function getLiveEditCount()
124
    {
125
        return $this->data['liveEditCount'];
126
    }
127
128
    /**
129
     * Get the total number of edits.
130
     * @return int
131
     */
132
    public function getTotalEditCount()
133
    {
134
        return $this->data['deletedEditCount'] + $this->data['liveEditCount'];
135
    }
136
137
    /**
138
     * Get the local user groups.
139
     * @return string[]
140
     */
141
    public function getUserGroups()
142
    {
143
        return $this->data['userGroups'];
144
    }
145
146
    /**
147
     * Get the global user groups.
148
     * @return string[]
149
     */
150
    public function getGlobalUserGroups()
151
    {
152
        return $this->data['globalUserGroups'];
153
    }
154
}
155