PageQueueManagement::create()   B
last analyzed

Complexity

Conditions 10
Paths 33

Size

Total Lines 73
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 50
c 3
b 0
f 0
dl 0
loc 73
rs 7.2242
cc 10
nc 33
nop 0

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 Waca\DataObjects\RequestQueue;
12
use Waca\DataObjects\User;
13
use Waca\Helpers\Logger;
14
use Waca\Helpers\RequestQueueHelper;
15
use Waca\SessionAlert;
16
use Waca\Tasks\InternalPageBase;
17
use Waca\WebRequest;
18
19
class PageQueueManagement extends InternalPageBase
20
{
21
    /** @var RequestQueueHelper */
22
    private $helper;
23
24
    public function __construct()
25
    {
26
        $this->helper = new RequestQueueHelper();
27
    }
28
29
    protected function main()
30
    {
31
        $this->setHtmlTitle('Request Queue Management');
32
33
        $database = $this->getDatabase();
34
        $queues = RequestQueue::getAllQueues($database);
35
36
        $this->assign('queues', $queues);
37
38
        $user = User::getCurrent($database);
39
        $this->assign('canCreate', $this->barrierTest('create', $user));
40
        $this->assign('canEdit', $this->barrierTest('edit', $user));
41
42
        $this->setTemplate('queue-management/main.tpl');
43
    }
44
45
    protected function create()
46
    {
47
        if (WebRequest::wasPosted()) {
48
            $this->validateCSRFToken();
49
            $database = $this->getDatabase();
50
51
            $queue = new RequestQueue();
52
53
            $queue->setDatabase($database);
54
            $queue->setDomain(1); // FIXME: domain
55
56
            $queue->setHeader(WebRequest::postString('header'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('header') can also be of type null; however, parameter $header of Waca\DataObjects\RequestQueue::setHeader() 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

56
            $queue->setHeader(/** @scrutinizer ignore-type */ WebRequest::postString('header'));
Loading history...
57
            $queue->setDisplayName(WebRequest::postString('displayName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('displayName') can also be of type null; however, parameter $displayName of Waca\DataObjects\RequestQueue::setDisplayName() 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

57
            $queue->setDisplayName(/** @scrutinizer ignore-type */ WebRequest::postString('displayName'));
Loading history...
58
            $queue->setApiName(WebRequest::postString('apiName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('apiName') can also be of type null; however, parameter $apiName of Waca\DataObjects\RequestQueue::setApiName() 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

58
            $queue->setApiName(/** @scrutinizer ignore-type */ WebRequest::postString('apiName'));
Loading history...
59
            $queue->setEnabled(WebRequest::postBoolean('enabled'));
60
            $queue->setDefault(WebRequest::postBoolean('default') && WebRequest::postBoolean('enabled'));
61
            $queue->setDefaultAntispoof(WebRequest::postBoolean('antispoof') && WebRequest::postBoolean('enabled'));
62
            $queue->setDefaultTitleBlacklist(WebRequest::postBoolean('titleblacklist') && WebRequest::postBoolean('enabled'));
63
            $queue->setHelp(WebRequest::postString('help'));
64
            $queue->setLogName(WebRequest::postString('logName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('logName') can also be of type null; however, parameter $logName of Waca\DataObjects\RequestQueue::setLogName() 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

64
            $queue->setLogName(/** @scrutinizer ignore-type */ WebRequest::postString('logName'));
Loading history...
Deprecated Code introduced by
The function Waca\DataObjects\RequestQueue::setLogName() has been deprecated. ( Ignorable by Annotation )

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

64
            /** @scrutinizer ignore-deprecated */ $queue->setLogName(WebRequest::postString('logName'));
Loading history...
65
66
            $proceed = true;
67
68
            if (RequestQueue::getByApiName($database, $queue->getApiName(), 1) !== false) {
69
                // FIXME: domain
70
                SessionAlert::error("The chosen API name is already in use. Please choose another.");
71
                $proceed = false;
72
            }
73
74
            if (preg_match('/^[A-Za-z][a-zA-Z0-9_-]*$/', $queue->getApiName()) !== 1) {
75
                SessionAlert::error("The chosen API name contains invalid characters");
76
                $proceed = false;
77
            }
78
79
            if (RequestQueue::getByDisplayName($database, $queue->getDisplayName(), 1) !== false) {
80
                // FIXME: domain
81
                SessionAlert::error("The chosen target display name is already in use. Please choose another.");
82
                $proceed = false;
83
            }
84
85
            if (RequestQueue::getByHeader($database, $queue->getHeader(), 1) !== false) {
86
                // FIXME: domain
87
                SessionAlert::error("The chosen header is already in use. Please choose another.");
88
                $proceed = false;
89
            }
90
91
            if ($proceed) {
92
                $queue->save();
93
                Logger::requestQueueCreated($database, $queue);
94
                $this->redirect('queueManagement');
95
            }
96
            else {
97
                $this->populateFromObject($queue);
98
99
                $this->assign('createMode', true);
100
                $this->setTemplate('queue-management/edit.tpl');
101
            }
102
        }
103
        else {
104
            $this->assign('header', null);
105
            $this->assign('displayName', null);
106
            $this->assign('apiName', null);
107
            $this->assign('enabled', false);
108
            $this->assign('antispoof', false);
109
            $this->assign('isTarget', false);
110
            $this->assign('titleblacklist', false);
111
            $this->assign('default', false);
112
            $this->assign('help', null);
113
            $this->assign('logName', null);
114
115
            $this->assignCSRFToken();
116
            $this->assign('createMode', true);
117
            $this->setTemplate('queue-management/edit.tpl');
118
        }
119
    }
120
121
    protected function edit()
122
    {
123
        $database = $this->getDatabase();
124
125
        /** @var RequestQueue $queue */
126
        $queue = RequestQueue::getById(WebRequest::getInt('queue'), $database);
127
128
        if (WebRequest::wasPosted()) {
129
            $this->validateCSRFToken();
130
131
            $this->helper->configureDefaults(
132
                $queue,
133
                WebRequest::postBoolean('enabled'),
134
                WebRequest::postBoolean('default'),
135
                WebRequest::postBoolean('antispoof'),
136
                WebRequest::postBoolean('titleblacklist'),
137
                $this->helper->isEmailTemplateTarget($queue, $this->getDatabase()) || $this->helper->isRequestFormTarget($queue, $this->getDatabase()));
138
139
            $queue->setHeader(WebRequest::postString('header'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('header') can also be of type null; however, parameter $header of Waca\DataObjects\RequestQueue::setHeader() 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

139
            $queue->setHeader(/** @scrutinizer ignore-type */ WebRequest::postString('header'));
Loading history...
140
            $queue->setDisplayName(WebRequest::postString('displayName'));
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('displayName') can also be of type null; however, parameter $displayName of Waca\DataObjects\RequestQueue::setDisplayName() 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
            $queue->setDisplayName(/** @scrutinizer ignore-type */ WebRequest::postString('displayName'));
Loading history...
141
            $queue->setHelp(WebRequest::postString('help'));
142
143
            $proceed = true;
144
145
            $foundRequestQueue = RequestQueue::getByDisplayName($database, $queue->getDisplayName(), 1);
146
            if ($foundRequestQueue !== false && $foundRequestQueue->getId() !== $queue->getId()) {
147
                // FIXME: domain
148
                SessionAlert::error("The chosen target display name is already in use. Please choose another.");
149
                $proceed = false;
150
            }
151
152
            $foundRequestQueue = RequestQueue::getByHeader($database, $queue->getHeader(), 1);
153
            if ($foundRequestQueue !== false && $foundRequestQueue->getId() !== $queue->getId()) {
154
                // FIXME: domain
155
                SessionAlert::error("The chosen header is already in use. Please choose another.");
156
                $proceed = false;
157
            }
158
159
            if ($proceed) {
160
                Logger::requestQueueEdited($database, $queue);
161
                $queue->save();
162
                $this->redirect('queueManagement');
163
            }
164
            else {
165
                $this->populateFromObject($queue);
166
167
                $this->assign('createMode', false);
168
                $this->setTemplate('queue-management/edit.tpl');
169
            }
170
        }
171
        else {
172
            $this->populateFromObject($queue);
173
174
            $this->assign('createMode', false);
175
            $this->setTemplate('queue-management/edit.tpl');
176
        }
177
    }
178
179
    /**
180
     * @param RequestQueue $queue
181
     */
182
    protected function populateFromObject(RequestQueue $queue): void
183
    {
184
        $this->assignCSRFToken();
185
186
        $this->assign('header', $queue->getHeader());
187
        $this->assign('displayName', $queue->getDisplayName());
188
        $this->assign('apiName', $queue->getApiName());
189
        $this->assign('enabled', $queue->isEnabled());
190
        $this->assign('default', $queue->isDefault());
191
        $this->assign('antispoof', $queue->isDefaultAntispoof());
192
        $this->assign('titleblacklist', $queue->isDefaultTitleBlacklist());
193
        $this->assign('help', $queue->getHelp());
194
        $this->assign('logName', $queue->getLogName());
0 ignored issues
show
Deprecated Code introduced by
The function Waca\DataObjects\RequestQueue::getLogName() has been deprecated. ( Ignorable by Annotation )

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

194
        $this->assign('logName', /** @scrutinizer ignore-deprecated */ $queue->getLogName());
Loading history...
195
196
        $isQueueTarget = $this->helper->isEmailTemplateTarget($queue, $this->getDatabase());
197
        $isFormTarget = $this->helper->isRequestFormTarget($queue, $this->getDatabase());
198
        $this->assign('isTarget', $isQueueTarget);
199
        $this->assign('isFormTarget', $isFormTarget);
200
    }
201
}