Passed
Push — autoskin ( 7712b8...a8eb4a )
by Simon
03:41
created

PagePreferences::main()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 54
ccs 0
cts 43
cp 0
rs 8.0835
cc 8
nc 6
nop 0
crap 72

How to fix   Long Method   

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\UserAuth;
10
11
use Waca\DataObjects\User;
12
use Waca\Helpers\OAuthUserHelper;
13
use Waca\SessionAlert;
14
use Waca\Tasks\InternalPageBase;
15
use Waca\WebRequest;
16
17
class PagePreferences extends InternalPageBase
18
{
19
    /**
20
     * Main function for this page, when no specific actions are called.
21
     * @return void
22
     */
23
    protected function main()
24
    {
25
        $this->setHtmlTitle('Preferences');
26
27
        $enforceOAuth = $this->getSiteConfiguration()->getEnforceOAuth();
28
        $database = $this->getDatabase();
29
        $user = User::getCurrent($database);
30
31
        // Dual mode
32
        if (WebRequest::wasPosted()) {
33
            $this->validateCSRFToken();
34
            $user->setWelcomeSig(WebRequest::postString('sig'));
35
            $user->setEmailSig(WebRequest::postString('emailsig'));
36
            $user->setAbortPref(WebRequest::postBoolean('abortpref') ? 1 : 0);
37
            $this->setCreationMode($user);
38
39
            $newSkin = WebRequest::postString('skintype');
40
            if ($newSkin === 'main' || $newSkin === 'alt' || $newSkin === 'auto') {
41
                $user->setSkin($newSkin);
42
            }
43
44
            $email = WebRequest::postEmail('email');
45
            if ($email !== null) {
46
                $user->setEmail($email);
47
            }
48
49
            $user->save();
50
            SessionAlert::success("Preferences updated!");
51
52
            $this->redirect('');
53
        }
54
        else {
55
            $this->assignCSRFToken();
56
            $this->setTemplate('preferences/prefs.tpl');
57
            $this->assign("enforceOAuth", $enforceOAuth);
58
59
            $this->assign('canManualCreate',
60
                $this->barrierTest(User::CREATION_MANUAL, $user, 'RequestCreation'));
61
            $this->assign('canOauthCreate',
62
                $this->barrierTest(User::CREATION_OAUTH, $user, 'RequestCreation'));
63
            $this->assign('canBotCreate',
64
                $this->barrierTest(User::CREATION_BOT, $user, 'RequestCreation'));
65
66
            $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(),
67
                $this->getSiteConfiguration());
68
            $this->assign('oauth', $oauth);
69
70
            $identity = null;
71
            if ($oauth->isFullyLinked()) {
72
                $identity = $oauth->getIdentity();
73
            }
74
75
            $this->assign('identity', $identity);
76
            $this->assign('graceTime', $this->getSiteConfiguration()->getOauthIdentityGraceTime());
77
        }
78
    }
79
80
    protected function refreshOAuth()
81
    {
82
        if (!WebRequest::wasPosted()) {
83
            $this->redirect('preferences');
84
85
            return;
86
        }
87
88
        $database = $this->getDatabase();
89
        $oauth = new OAuthUserHelper(User::getCurrent($database), $database, $this->getOAuthProtocolHelper(),
90
            $this->getSiteConfiguration());
91
        if ($oauth->isFullyLinked()) {
92
            $oauth->refreshIdentity();
93
        }
94
95
        $this->redirect('preferences');
96
97
        return;
98
    }
99
100
    /**
101
     * @param User $user
102
     */
103
    protected function setCreationMode(User $user)
104
    {
105
        // if the user is selecting a creation mode that they are not allowed, do nothing.
106
        // this has the side effect of allowing them to keep a selected mode that either has been changed for them,
107
        // or that they have kept from when they previously had certain access.
108
        $creationMode = WebRequest::postInt('creationmode');
109
        if ($this->barrierTest($creationMode, $user, 'RequestCreation')) {
110
            $user->setCreationMode($creationMode);
111
        }
112
    }
113
}
114