Failed Conditions
Push — rbac ( be68b4...52c28b )
by Michael
03:11
created

PageViewRequest::setupLogData()   C

Complexity

Conditions 11
Paths 182

Size

Total Lines 67
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 67
ccs 0
cts 55
cp 0
rs 6.6333
cc 11
nc 182
nop 2
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Exception;
12
use Waca\DataObjects\Comment;
13
use Waca\DataObjects\EmailTemplate;
14
use Waca\DataObjects\Log;
15
use Waca\DataObjects\Request;
16
use Waca\DataObjects\User;
17
use Waca\Exceptions\ApplicationLogicException;
18
use Waca\Fragments\RequestData;
19
use Waca\Helpers\LogHelper;
20
use Waca\Helpers\SearchHelpers\UserSearchHelper;
21
use Waca\PdoDatabase;
22
use Waca\Tasks\InternalPageBase;
23
use Waca\WebRequest;
24
25
class PageViewRequest extends InternalPageBase
26
{
27
    use RequestData;
28
    const STATUS_SYMBOL_OPEN = '&#x2610';
29
    const STATUS_SYMBOL_ACCEPTED = '&#x2611';
30
    const STATUS_SYMBOL_REJECTED = '&#x2612';
31
32
    /**
33
     * Main function for this page, when no specific actions are called.
34
     * @throws ApplicationLogicException
35
     */
36
    protected function main()
37
    {
38
        // set up csrf protection
39
        $this->assignCSRFToken();
40
41
        // get some useful objects
42
        $database = $this->getDatabase();
43
        $request = $this->getRequest($database, WebRequest::getInt('id'));
44
        $config = $this->getSiteConfiguration();
45
        $currentUser = User::getCurrent($database);
46
47
        // Test we should be able to look at this request
48
        if ($config->getEmailConfirmationEnabled()) {
49
            if ($request->getEmailConfirm() !== 'Confirmed') {
50
                // Not allowed to look at this yet.
51
                throw new ApplicationLogicException('The email address has not yet been confirmed for this request.');
52
            }
53
        }
54
55
        $this->setupBasicData($request, $config);
56
57
        $this->setupUsernameData($request);
58
59
        $this->setupTitle($request);
60
61
        $this->setupReservationDetails($request->getReserved(), $database, $currentUser);
62
        $this->setupGeneralData($database);
63
64
        $this->assign('requestDataCleared', false);
65
        if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) {
66
            $this->assign('requestDataCleared', true);
67
        }
68
69
        $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser);
70
71
        $this->setupLogData($request, $database);
72
73
        if ($allowedPrivateData) {
74
            $this->setTemplate('view-request/main-with-data.tpl');
75
            $this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database);
76
77
            $this->assign('canSetBan', $this->barrierTest('set', $currentUser, PageBan::class));
78
            $this->assign('canSeeCheckuserData', $this->barrierTest('seeUserAgentData', $currentUser, 'RequestData'));
79
80
            if ($this->barrierTest('seeUserAgentData', $currentUser, 'RequestData')) {
81
                $this->setTemplate('view-request/main-with-checkuser-data.tpl');
82
                $this->setupCheckUserData($request);
83
            }
84
        }
85
        else {
86
            $this->setTemplate('view-request/main.tpl');
87
        }
88
    }
0 ignored issues
show
Coding Style introduced by
Expected //end main()
Loading history...
89
90
    /**
91
     * @param Request $request
92
     */
93
    protected function setupTitle(Request $request)
94
    {
95
        $statusSymbol = self::STATUS_SYMBOL_OPEN;
96
        if ($request->getStatus() === 'Closed') {
97
            if ($request->getWasCreated()) {
98
                $statusSymbol = self::STATUS_SYMBOL_ACCEPTED;
99
            }
100
            else {
101
                $statusSymbol = self::STATUS_SYMBOL_REJECTED;
102
            }
103
        }
104
105
        $this->setHtmlTitle($statusSymbol . ' #' . $request->getId());
106
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setupTitle()
Loading history...
107
108
    /**
109
     * Sets up data unrelated to the request, such as the email template information
110
     *
111
     * @param PdoDatabase $database
112
     */
113
    protected function setupGeneralData(PdoDatabase $database)
114
    {
115
        $config = $this->getSiteConfiguration();
116
117
        $this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #');
118
119
        $this->assign('defaultRequestState', $config->getDefaultRequestStateKey());
120
121
        $this->assign('requestStates', $config->getRequestStates());
122
123
        /** @var EmailTemplate $createdTemplate */
124
        $createdTemplate = EmailTemplate::getById($config->getDefaultCreatedTemplateId(), $database);
125
126
        $this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != '');
127
        $this->assign('createdJsQuestion', $createdTemplate->getJsquestion());
128
        $this->assign('createdId', $createdTemplate->getId());
129
        $this->assign('createdName', $createdTemplate->getName());
130
131
        $createReasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED, $database);
132
        $this->assign("createReasons", $createReasons);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal createReasons does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
133
        $declineReasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED, $database);
134
        $this->assign("declineReasons", $declineReasons);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal declineReasons does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
135
136
        $allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED, $database);
137
        $this->assign("allCreateReasons", $allCreateReasons);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal allCreateReasons does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
138
        $allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED, $database);
139
        $this->assign("allDeclineReasons", $allDeclineReasons);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal allDeclineReasons does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
140
        $allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database);
141
        $this->assign("allOtherReasons", $allOtherReasons);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal allOtherReasons does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
142
143
        $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
144
            return UserSearchHelper::get($database)->byStatus('Active')->fetchColumn('username');
145
        });
146
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setupGeneralData()
Loading history...
147
148
    private function setupLogData(Request $request, PdoDatabase $database)
149
    {
150
        $currentUser = User::getCurrent($database);
151
152
        $logs = LogHelper::getRequestLogsWithComments($request->getId(), $database, $this->getSecurityManager());
153
        $requestLogs = array();
154
155
        if (trim($request->getComment()) !== "") {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
156
            $requestLogs[] = array(
157
                'type'     => 'comment',
158
                'security' => 'user',
159
                'userid'   => null,
160
                'user'     => $request->getName(),
161
                'entry'    => null,
162
                'time'     => $request->getDate(),
163
                'canedit'  => false,
164
                'id'       => $request->getId(),
165
                'comment'  => $request->getComment(),
166
            );
167
        }
168
169
        /** @var User[] $nameCache */
170
        $nameCache = array();
171
172
        $editableComments = $this->barrierTest('editOthers', $currentUser, PageEditComment::class);
173
174
        /** @var Log|Comment $entry */
175
        foreach ($logs as $entry) {
176
            // both log and comment have a 'user' field
177
            if (!array_key_exists($entry->getUser(), $nameCache)) {
0 ignored issues
show
Bug introduced by
The method getUser() does not exist on Waca\DataObject. It seems like you code against a sub-type of Waca\DataObject such as Waca\DataObjects\Log or Waca\DataObjects\Ban or Waca\DataObjects\UserRole or Waca\DataObjects\Comment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
            if (!array_key_exists($entry->/** @scrutinizer ignore-call */ getUser(), $nameCache)) {
Loading history...
178
                $entryUser = User::getById($entry->getUser(), $database);
179
                $nameCache[$entry->getUser()] = $entryUser;
180
            }
181
182
            if ($entry instanceof Comment) {
183
                $requestLogs[] = array(
184
                    'type'     => 'comment',
185
                    'security' => $entry->getVisibility(),
186
                    'user'     => $nameCache[$entry->getUser()]->getUsername(),
187
                    'userid'   => $entry->getUser() == -1 ? null : $entry->getUser(),
188
                    'entry'    => null,
189
                    'time'     => $entry->getTime(),
190
                    'canedit'  => ($editableComments || $entry->getUser() == $currentUser->getId()),
191
                    'id'       => $entry->getId(),
192
                    'comment'  => $entry->getComment(),
193
                );
194
            }
195
196
            if ($entry instanceof Log) {
197
                $invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0;
198
                $entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()];
199
200
                $requestLogs[] = array(
201
                    'type'     => 'log',
202
                    'security' => 'user',
203
                    'userid'   => $entry->getUser() == -1 ? null : $entry->getUser(),
204
                    'user'     => $entryUser->getUsername(),
205
                    'entry'    => LogHelper::getLogDescription($entry),
206
                    'time'     => $entry->getTimestamp(),
207
                    'canedit'  => false,
208
                    'id'       => $entry->getId(),
209
                    'comment'  => $entry->getComment(),
210
                );
211
            }
212
        }
213
214
        $this->assign("requestLogs", $requestLogs);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal requestLogs does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
215
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setupLogData()
Loading history...
216
217
    /**
218
     * @param Request $request
219
     */
220
    protected function setupUsernameData(Request $request)
221
    {
222
        $blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName());
223
224
        $this->assign('requestIsBlacklisted', $blacklistData !== false);
225
        $this->assign('requestBlacklist', $blacklistData);
226
227
        try {
228
            $spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName());
229
        }
230
        catch (Exception $ex) {
231
            $spoofs = $ex->getMessage();
232
        }
233
234
        $this->assign("spoofs", $spoofs);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal spoofs does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
235
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setupUsernameData()
Loading history...
236
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
237