PageDomainManagement::edit()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 64
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 64
rs 8.6097
cc 6
nc 5
nop 0

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;
10
11
use Waca\DataObjects\Domain;
12
use Waca\DataObjects\EmailTemplate;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\AccessDeniedException;
15
use Waca\Helpers\Logger;
16
use Waca\SessionAlert;
17
use Waca\Tasks\InternalPageBase;
18
use Waca\WebRequest;
19
20
class PageDomainManagement extends InternalPageBase
21
{
22
    protected function main()
23
    {
24
        $this->setHtmlTitle('Domain Management');
25
26
        $database = $this->getDatabase();
27
        $currentUser = User::getCurrent($database);
28
29
        /** @var Domain[] $domains */
30
        $domains = Domain::getAll($database);
31
32
        $templates = [];
33
        foreach ($domains as $domain) {
34
            if ($domain->getDefaultClose() !== null) {
35
                $templates[$domain->getDefaultClose()] = EmailTemplate::getById($domain->getDefaultClose(), $database);
36
            }
37
        }
38
39
        $canEdit = $this->barrierTest('edit', $currentUser);
40
        $canEditAll = $this->barrierTest('editAll', $currentUser);
41
        $canCreate = $this->barrierTest('create', $currentUser);
42
        $this->assign('canEdit', $canEdit);
43
        $this->assign('canEditAll', $canEditAll);
44
        $this->assign('canCreate', $canCreate);
45
46
        $this->assign('domains', $domains);
47
        $this->assign('closeTemplates', $templates);
48
        $this->assign('currentDomain', Domain::getCurrent($database));
49
        $this->setTemplate('domain-management/main.tpl');
50
    }
51
52
    protected function create()
53
    {
54
        $this->setHtmlTitle('Domain Management');
55
        $database = $this->getDatabase();
56
        $currentUser = User::getCurrent($database);
57
58
        // quickly check the user is allowed to edit all fields. If not, then they shouldn't be allowed to create
59
        // new domains either. With any luck, a competent developer would never grant create without editAll to a role
60
        // anyway, so this will never be hit.
61
        if (!$this->barrierTest('editAll', $currentUser)) {
62
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
63
        }
64
65
        if (WebRequest::wasPosted()) {
66
            $this->validateCSRFToken();
67
68
            $domain = new Domain();
69
            $domain->setDatabase($database);
70
71
            $domain->setShortName(WebRequest::postString('shortName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('shortName') can also be of type null; however, parameter $shortName of Waca\DataObjects\Domain::setShortName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

71
            $domain->setShortName(/** @scrutinizer ignore-type */ WebRequest::postString('shortName'));
Loading history...
72
            $domain->setLongName(WebRequest::postString('longName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('longName') can also be of type null; however, parameter $longName of Waca\DataObjects\Domain::setLongName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

72
            $domain->setLongName(/** @scrutinizer ignore-type */ WebRequest::postString('longName'));
Loading history...
73
            $domain->setWikiArticlePath(WebRequest::postString('articlePath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('articlePath') can also be of type null; however, parameter $wikiArticlePath of Waca\DataObjects\Domain::setWikiArticlePath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

73
            $domain->setWikiArticlePath(/** @scrutinizer ignore-type */ WebRequest::postString('articlePath'));
Loading history...
74
            $domain->setWikiApiPath(WebRequest::postString('apiPath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('apiPath') can also be of type null; however, parameter $wikiApiPath of Waca\DataObjects\Domain::setWikiApiPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
            $domain->setWikiApiPath(/** @scrutinizer ignore-type */ WebRequest::postString('apiPath'));
Loading history...
75
            $domain->setEnabled(WebRequest::postBoolean('enabled'));
76
            $domain->setDefaultLanguage(WebRequest::postString('defaultLanguage'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('defaultLanguage') can also be of type null; however, parameter $defaultLanguage of Waca\DataObjects\Domain::setDefaultLanguage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            $domain->setDefaultLanguage(/** @scrutinizer ignore-type */ WebRequest::postString('defaultLanguage'));
Loading history...
77
            $domain->setDefaultClose(null);
78
            $domain->setEmailReplyAddress(WebRequest::postString('emailReplyTo'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('emailReplyTo') can also be of type null; however, parameter $emailReplyAddress of Waca\DataObjects\Domain::setEmailReplyAddress() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

78
            $domain->setEmailReplyAddress(/** @scrutinizer ignore-type */ WebRequest::postString('emailReplyTo'));
Loading history...
79
            $domain->setNotificationTarget(WebRequest::postString('notificationTarget'));
80
            $domain->setLocalDocumentation(WebRequest::postString('localDocumentation'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('localDocumentation') can also be of type null; however, parameter $localDocumentation of Waca\DataObjects\Domain::setLocalDocumentation() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

80
            $domain->setLocalDocumentation(/** @scrutinizer ignore-type */ WebRequest::postString('localDocumentation'));
Loading history...
81
82
            $domain->save();
83
84
            Logger::domainCreated($database, $domain);
85
            $this->redirect('domainManagement');
86
        }
87
        else {
88
            $this->assignCSRFToken();
89
90
            $this->assign('shortName', '');
91
            $this->assign('longName', '');
92
            $this->assign('articlePath', '');
93
            $this->assign('apiPath', '');
94
            $this->assign('enabled', false);
95
            $this->assign('defaultLanguage', 'en');
96
            $this->assign('emailReplyTo', '');
97
            $this->assign('notificationTarget', '');
98
            $this->assign('localDocumentation', '');
99
100
            $this->assign('createMode', true);
101
            $this->assign('canEditAll', true);
102
103
            $this->setTemplate('domain-management/edit.tpl');
104
        }
105
    }
106
107
    protected function edit()
108
    {
109
        $this->setHtmlTitle('Domain Management');
110
        $database = $this->getDatabase();
111
        $currentUser = User::getCurrent($database);
112
113
        $canEditAll = $this->barrierTest('editAll', $currentUser);
114
115
        /** @var Domain $domain */
116
        $domain = Domain::getById(WebRequest::getInt('domain'), $database);
117
118
        if (WebRequest::wasPosted()) {
119
            $this->validateCSRFToken();
120
121
            $domain->setLongName(WebRequest::postString('longName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('longName') can also be of type null; however, parameter $longName of Waca\DataObjects\Domain::setLongName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

121
            $domain->setLongName(/** @scrutinizer ignore-type */ WebRequest::postString('longName'));
Loading history...
122
            $domain->setDefaultLanguage(WebRequest::postString('defaultLanguage'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('defaultLanguage') can also be of type null; however, parameter $defaultLanguage of Waca\DataObjects\Domain::setDefaultLanguage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

122
            $domain->setDefaultLanguage(/** @scrutinizer ignore-type */ WebRequest::postString('defaultLanguage'));
Loading history...
123
            $domain->setLocalDocumentation(WebRequest::postString('localDocumentation'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('localDocumentation') can also be of type null; however, parameter $localDocumentation of Waca\DataObjects\Domain::setLocalDocumentation() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

123
            $domain->setLocalDocumentation(/** @scrutinizer ignore-type */ WebRequest::postString('localDocumentation'));
Loading history...
124
125
            /** @var EmailTemplate $template */
126
            $template = EmailTemplate::getById(WebRequest::postInt('defaultClose'), $database);
127
            if ($template->getActive()
128
                && $template->getPreloadOnly() === false
129
                && $template->getDefaultAction() === EmailTemplate::ACTION_CREATED) {
130
                $domain->setDefaultClose(WebRequest::postInt('defaultClose'));
131
            }
132
            else {
133
                SessionAlert::warning("Chosen email template is not valid for use as the default creation template");
134
            }
135
136
            if ($canEditAll) {
137
                $domain->setWikiArticlePath(WebRequest::postString('articlePath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('articlePath') can also be of type null; however, parameter $wikiArticlePath of Waca\DataObjects\Domain::setWikiArticlePath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

137
                $domain->setWikiArticlePath(/** @scrutinizer ignore-type */ WebRequest::postString('articlePath'));
Loading history...
138
                $domain->setWikiApiPath(WebRequest::postString('apiPath'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('apiPath') can also be of type null; however, parameter $wikiApiPath of Waca\DataObjects\Domain::setWikiApiPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

138
                $domain->setWikiApiPath(/** @scrutinizer ignore-type */ WebRequest::postString('apiPath'));
Loading history...
139
                $domain->setEnabled(WebRequest::postBoolean('enabled'));
140
                $domain->setEmailReplyAddress(WebRequest::postString('emailReplyTo'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('emailReplyTo') can also be of type null; however, parameter $emailReplyAddress of Waca\DataObjects\Domain::setEmailReplyAddress() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

140
                $domain->setEmailReplyAddress(/** @scrutinizer ignore-type */ WebRequest::postString('emailReplyTo'));
Loading history...
141
                $domain->setNotificationTarget(WebRequest::postString('notificationTarget'));
142
            }
143
144
            $domain->save();
145
146
            Logger::domainEdited($database, $domain);
147
            $this->redirect('domainManagement');
148
        }
149
        else {
150
            $this->assignCSRFToken();
151
152
            $templates = EmailTemplate::getActiveNonpreloadTemplates(EmailTemplate::ACTION_CREATED, $database);
153
            $this->assign('closeTemplates', $templates);
154
155
            $this->assign('shortName', $domain->getShortName());
156
            $this->assign('longName', $domain->getLongName());
157
            $this->assign('articlePath', $domain->getWikiArticlePath());
158
            $this->assign('apiPath', $domain->getWikiApiPath());
159
            $this->assign('enabled', $domain->isEnabled());
160
            $this->assign('defaultClose', $domain->getDefaultClose());
161
            $this->assign('defaultLanguage', $domain->getDefaultLanguage());
162
            $this->assign('emailReplyTo', $domain->getEmailReplyAddress());
163
            $this->assign('notificationTarget', $domain->getNotificationTarget());
164
            $this->assign('localDocumentation', $domain->getLocalDocumentation());
165
166
167
            $this->assign('createMode', false);
168
            $this->assign('canEditAll', $canEditAll);
169
170
            $this->setTemplate('domain-management/edit.tpl');
171
        }
172
    }
173
}
174