Completed
Push — master ( 8d2667...a78947 )
by MusikAnimal
04:28
created

SimpleEditCounter::getDeletedEditCount()   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, and we'd need to move
13
 * the single query to a repo class just so we could mock 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 array The Simple Edit Counter results. */
28
    protected $data = [
29
        'userId' => null,
30
        'deletedEditCount' => 0,
31
        'liveEditCount' => 0,
32
        'userGroups' => [],
33
        'globalUserGroups' => [],
34
    ];
35
36
    /**
37
     * Constructor for the SimpleEditCounter class.
38
     * @param Container $container The DI container.
39
     * @param Project   $project
40
     * @param User      $user
41
     */
42
    public function __construct(Container $container, Project $project, User $user)
43
    {
44
        $this->container = $container;
45
        $this->project = $project;
46
        $this->user = $user;
47
    }
48
49
    /**
50
     * Fetch the data from the database and API,
51
     * then set class properties with the values.
52
     */
53
    public function prepareData()
54
    {
55
        $results = $this->fetchData();
56
57
        // Iterate over the results, putting them in the right variables
58
        foreach ($results as $row) {
59
            switch ($row['source']) {
60
                case 'id':
61
                    $this->data['userId'] = $row['value'];
62
                    break;
63
                case 'arch':
64
                    $this->data['deletedEditCount'] = $row['value'];
65
                    break;
66
                case 'rev':
67
                    $this->data['liveEditCount'] = $row['value'];
68
                    break;
69
                case 'groups':
70
                    $this->data['userGroups'][] = $row['value'];
71
                    break;
72
            }
73
        }
74
75
        if (!$this->container->getParameter('app.single_wiki')) {
76
            $this->data['globalUserGroups'] = $this->user->getGlobalGroups($this->project);
77
        }
78
    }
79
80
    /**
81
     * Run the query against the database.
82
     * @return string[]
83
     */
84
    private function fetchData()
85
    {
86
        $userTable = $this->project->getTableName('user');
87
        $archiveTable = $this->project->getTableName('archive');
88
        $revisionTable = $this->project->getTableName('revision');
89
        $userGroupsTable = $this->project->getTableName('user_groups');
90
91
        /** @var Connection $conn */
92
        $conn = $this->container->get('doctrine')->getManager('replicas')->getConnection();
93
94
        // Prepare the query and execute
95
        $resultQuery = $conn->prepare("
96
            SELECT 'id' AS source, user_id as value
97
                FROM $userTable
98
                WHERE user_name = :username
99
            UNION
100
            SELECT 'arch' AS source, COUNT(*) AS value
101
                FROM $archiveTable
102
                WHERE ar_user_text = :username
103
            UNION
104
            SELECT 'rev' AS source, COUNT(*) AS value
105
                FROM $revisionTable
106
                WHERE rev_user_text = :username
107
            UNION
108
            SELECT 'groups' AS source, ug_group AS value
109
                FROM $userGroupsTable
110
                JOIN $userTable ON user_id = ug_user
111
                WHERE user_name = :username
112
        ");
113
114
        $username = $this->user->getUsername();
115
        $resultQuery->bindParam('username', $username);
116
        $resultQuery->execute();
117
118
        // Fetch the result data
119
        return $resultQuery->fetchAll();
120
    }
121
122
    /**
123
     * Get back all the data as a single associative array.
124
     * @return array
125
     */
126
    public function getData()
127
    {
128
        return $this->data;
129
    }
130
131
    /**
132
     * Get the user's ID.
133
     * @return int
134
     */
135
    public function getUserId()
136
    {
137
        return $this->data['userId'];
138
    }
139
140
    /**
141
     * Get the number of deleted edits.
142
     * @return int
143
     */
144
    public function getDeletedEditCount()
145
    {
146
        return $this->data['deletedEditCount'];
147
    }
148
149
    /**
150
     * Get the number of live edits.
151
     * @return int
152
     */
153
    public function getLiveEditCount()
154
    {
155
        return $this->data['liveEditCount'];
156
    }
157
158
    /**
159
     * Get the total number of edits.
160
     * @return int
161
     */
162
    public function getTotalEditCount()
163
    {
164
        return $this->data['deletedEditCount'] + $this->data['liveEditCount'];
165
    }
166
167
    /**
168
     * Get the local user groups.
169
     * @return string[]
170
     */
171
    public function getUserGroups()
172
    {
173
        return $this->data['userGroups'];
174
    }
175
176
    /**
177
     * Get the global user groups.
178
     * @return string[]
179
     */
180
    public function getGlobalUserGroups()
181
    {
182
        return $this->data['globalUserGroups'];
183
    }
184
}
185