Failed Conditions
Push — newinternal ( b66232...216d62 )
by Simon
16:33 queued 06:35
created

PageMain   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 168
Duplicated Lines 31.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 53
loc 168
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 36 2
A setupLastFiveClosedData() 0 23 2
A setupHospitalQueue() 27 27 3
A setupJobQueue() 26 26 3
A setupStatusSections() 0 25 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use PDO;
12
use Waca\DataObjects\User;
13
use Waca\Helpers\SearchHelpers\RequestSearchHelper;
14
use Waca\Helpers\SearchHelpers\UserSearchHelper;
15
use Waca\Pages\RequestAction\PageBreakReservation;
16
use Waca\PdoDatabase;
17
use Waca\RequestStatus;
18
use Waca\SiteConfiguration;
19
use Waca\Tasks\InternalPageBase;
20
21
class PageMain extends InternalPageBase
22
{
23
    /**
24
     * Main function for this page, when no actions are called.
25
     */
26
    protected function main()
27
    {
28
        $this->assignCSRFToken();
29
30
        $config = $this->getSiteConfiguration();
31
        $database = $this->getDatabase();
32
        $currentUser = User::getCurrent($database);
33
34
        // general template configuration
35
        $this->assign('defaultRequestState', $config->getDefaultRequestStateKey());
36
        $this->assign('requestLimitShowOnly', $config->getMiserModeLimit());
37
38
        // Get map of possible usernames
39
        $userList = UserSearchHelper::get($database)->withReservedRequest();
40
        $this->assign('userList', $userList);
41
42
        $seeAllRequests = $this->barrierTest('seeAllRequests', $currentUser, PageViewRequest::class);
43
44
        // Fetch request data
45
        $requestSectionData = array();
46
        if ($seeAllRequests) {
47
            $this->setupStatusSections($database, $config, $requestSectionData);
48
            $this->setupHospitalQueue($database, $config, $requestSectionData);
49
            $this->setupJobQueue($database, $config, $requestSectionData);
50
        }
51
        $this->setupLastFiveClosedData($database, $seeAllRequests);
52
53
        // Assign data to template
54
        $this->assign('requestSectionData', $requestSectionData);
55
56
        // Extra rights
57
        $this->assign('canBan', $this->barrierTest('set', $currentUser, PageBan::class));
58
        $this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class));
59
60
        $this->setTemplate('mainpage/mainpage.tpl');
61
    }
62
63
    /**
64
     * @param PdoDatabase $database
65
     * @param bool        $seeAllRequests
66
     *
67
     * @internal param User $currentUser
68
     */
69
    private function setupLastFiveClosedData(PdoDatabase $database, $seeAllRequests)
70
    {
71
        $this->assign('showLastFive', $seeAllRequests);
72
        if (!$seeAllRequests) {
73
            return;
74
        }
75
76
        $query = <<<SQL
77
		SELECT request.id, request.name, request.updateversion
78
		FROM request /* PageMain::main() */
79
		JOIN log ON log.objectid = request.id AND log.objecttype = 'Request'
80
		WHERE log.action LIKE 'Closed%'
81
		ORDER BY log.timestamp DESC
82
		LIMIT 5;
83
SQL;
84
85
        $statement = $database->prepare($query);
86
        $statement->execute();
87
88
        $last5result = $statement->fetchAll(PDO::FETCH_ASSOC);
89
90
        $this->assign('lastFive', $last5result);
91
    }
92
93
    /**
94
     * @param PdoDatabase       $database
95
     * @param SiteConfiguration $config
96
     * @param                   $requestSectionData
97
     */
98 View Code Duplication
    private function setupHospitalQueue(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
        PdoDatabase $database,
100
        SiteConfiguration $config,
101
        &$requestSectionData
102
    ) {
103
        $search = RequestSearchHelper::get($database)
104
            ->limit($config->getMiserModeLimit())
105
            ->excludingStatus('Closed')
106
            ->isHospitalised();
107
108
        if ($config->getEmailConfirmationEnabled()) {
109
            $search->withConfirmedEmail();
110
        }
111
112
        $results = $search->getRecordCount($requestCount)->fetch();
113
114
        if($requestCount > 0) {
115
            $requestSectionData['Hospital - Requests failed auto-creation'] = array(
116
                'requests' => $results,
117
                'total'    => $requestCount,
118
                'api'      => 'hospital',
119
                'type'     => 'hospital',
120
                'special'  => 'Job Queue',
121
                'help'     => 'This queue lists all the requests which have been attempted to be created in the background, but for which this has failed for one reason or another. Check the job queue to find the error. Requests here may need to be created manually, or it may be possible to re-queue the request for auto-creation by the tool, or it may have been created already. Use your own technical discretion here.'
122
            );
123
        }
124
    }
125
126
    /**
127
     * @param PdoDatabase       $database
128
     * @param SiteConfiguration $config
129
     * @param                   $requestSectionData
130
     */
131 View Code Duplication
    private function setupJobQueue(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
        PdoDatabase $database,
133
        SiteConfiguration $config,
134
        &$requestSectionData
135
    ) {
136
        $search = RequestSearchHelper::get($database)
137
            ->limit($config->getMiserModeLimit())
138
            ->byStatus(RequestStatus::JOBQUEUE);
139
140
        if ($config->getEmailConfirmationEnabled()) {
141
            $search->withConfirmedEmail();
142
        }
143
144
        $results = $search->getRecordCount($requestCount)->fetch();
145
146
        if($requestCount > 0) {
147
            $requestSectionData['Requests queued in the Job Queue'] = array(
148
                'requests' => $results,
149
                'total'    => $requestCount,
150
                'api'      => 'JobQueue',
151
                'type'     => 'JobQueue',
152
                'special'  => 'Job Queue',
153
                'help'     => 'This section lists all the requests which are currently waiting to be created by the tool. Requests should automatically disappear from here within a few minutes.'
154
            );
155
        }
156
    }
157
158
    /**
159
     * @param PdoDatabase       $database
160
     * @param SiteConfiguration $config
161
     * @param                   $requestSectionData
162
     */
163
    private function setupStatusSections(
164
        PdoDatabase $database,
165
        SiteConfiguration $config,
166
        &$requestSectionData
167
    ) {
168
        $search = RequestSearchHelper::get($database)->limit($config->getMiserModeLimit())->notHospitalised();
169
170
        if ($config->getEmailConfirmationEnabled()) {
171
            $search->withConfirmedEmail();
172
        }
173
174
        $requestStates = $config->getRequestStates();
175
        $requestsByStatus = $search->fetchByStatus(array_keys($requestStates));
176
177
        foreach ($requestStates as $type => $v) {
178
            $requestSectionData[$v['header']] = array(
179
                'requests' => $requestsByStatus[$type]['data'],
180
                'total'    => $requestsByStatus[$type]['count'],
181
                'api'      => $v['api'],
182
                'type'     => $type,
183
                'special'  => null,
184
                'help'     => null,
185
            );
186
        }
187
    }
188
}
189