Passed
Pull Request — multiproject/db (#703)
by Simon
02:48
created

PageQueueManagement::main()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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
    protected function main()
22
    {
23
        $this->setHtmlTitle('Request Queue Management');
24
25
        $database = $this->getDatabase();
26
        $queues = RequestQueue::getAllQueues($database);
27
28
        $this->assign('queues', $queues);
29
30
        $user = User::getCurrent($database);
31
        $this->assign('canCreate', $this->barrierTest('create', $user));
32
        $this->assign('canEdit', $this->barrierTest('edit', $user));
33
34
        $this->setTemplate('queue-management/main.tpl');
35
    }
36
37
    protected function create()
38
    {
39
        if (WebRequest::wasPosted()) {
40
            $this->validateCSRFToken();
41
            $database = $this->getDatabase();
42
43
            $queue = new RequestQueue();
44
45
            $queue->setDatabase($database);
46
            $queue->setDomain(1); // FIXME: domain
47
48
            $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

48
            $queue->setHeader(/** @scrutinizer ignore-type */ WebRequest::postString('header'));
Loading history...
49
            $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

49
            $queue->setDisplayName(/** @scrutinizer ignore-type */ WebRequest::postString('displayName'));
Loading history...
50
            $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

50
            $queue->setApiName(/** @scrutinizer ignore-type */ WebRequest::postString('apiName'));
Loading history...
51
            $queue->setEnabled(WebRequest::postBoolean('enabled'));
52
            $queue->setDefault(WebRequest::postBoolean('default') && WebRequest::postBoolean('enabled'));
53
            $queue->setDefaultAntispoof(WebRequest::postBoolean('antispoof') && WebRequest::postBoolean('enabled'));
54
            $queue->setDefaultTitleBlacklist(WebRequest::postBoolean('titleblacklist') && WebRequest::postBoolean('enabled'));
55
            $queue->setHelp(WebRequest::postString('help'));
56
            $queue->setLogName(WebRequest::postString('logName'));
0 ignored issues
show
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

56
            /** @scrutinizer ignore-deprecated */ $queue->setLogName(WebRequest::postString('logName'));
Loading history...
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

56
            $queue->setLogName(/** @scrutinizer ignore-type */ WebRequest::postString('logName'));
Loading history...
57
            $queue->setLegacyStatus(WebRequest::postString('legacyStatus'));
0 ignored issues
show
Deprecated Code introduced by
The function Waca\DataObjects\RequestQueue::setLegacyStatus() 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

57
            /** @scrutinizer ignore-deprecated */ $queue->setLegacyStatus(WebRequest::postString('legacyStatus'));
Loading history...
Bug introduced by
It seems like Waca\WebRequest::postString('legacyStatus') can also be of type null; however, parameter $legacyStatus of Waca\DataObjects\RequestQueue::setLegacyStatus() 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->setLegacyStatus(/** @scrutinizer ignore-type */ WebRequest::postString('legacyStatus'));
Loading history...
58
59
            $proceed = true;
60
61
            if (RequestQueue::getByApiName($database, $queue->getApiName(), 1) !== false) {
62
                // FIXME: domain
63
                SessionAlert::error("The chosen API name is already in use. Please choose another.");
64
                $proceed = false;
65
            }
66
67
            if (RequestQueue::getByDisplayName($database, $queue->getDisplayName(), 1) !== false) {
68
                // FIXME: domain
69
                SessionAlert::error("The chosen target display name is already in use. Please choose another.");
70
                $proceed = false;
71
            }
72
73
            if (RequestQueue::getByHeader($database, $queue->getHeader(), 1) !== false) {
74
                // FIXME: domain
75
                SessionAlert::error("The chosen header is already in use. Please choose another.");
76
                $proceed = false;
77
            }
78
79
            if ($proceed) {
80
                $queue->save();
81
                Logger::requestQueueCreated($database, $queue);
82
                $this->redirect('queueManagement');
83
            }
84
            else {
85
                $this->populateFromObject($queue);
86
87
                $this->assign('createMode', true);
88
                $this->setTemplate('queue-management/edit.tpl');
89
            }
90
        }
91
        else {
92
            $this->assign('header', null);
93
            $this->assign('displayName', null);
94
            $this->assign('apiName', null);
95
            $this->assign('enabled', false);
96
            $this->assign('antispoof', false);
97
            $this->assign('titleblacklist', false);
98
            $this->assign('default', false);
99
            $this->assign('help', null);
100
            $this->assign('logName', null);
101
            $this->assign('legacyStatus', null);
102
103
            $this->assignCSRFToken();
104
            $this->assign('createMode', true);
105
            $this->setTemplate('queue-management/edit.tpl');
106
        }
107
    }
108
109
    protected function edit()
110
    {
111
        $database = $this->getDatabase();
112
113
        /** @var RequestQueue $queue */
114
        $queue = RequestQueue::getById(WebRequest::getInt('queue'), $database);
115
116
        if (WebRequest::wasPosted()) {
117
            $this->validateCSRFToken();
118
119
            $helper = new RequestQueueHelper();
120
121
            $helper->configureDefaults(
122
                $queue,
123
                WebRequest::postBoolean('enabled'),
124
                WebRequest::postBoolean('default'),
125
                WebRequest::postBoolean('antispoof'),
126
                WebRequest::postBoolean('titleblacklist'));
127
128
            $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

128
            $queue->setHeader(/** @scrutinizer ignore-type */ WebRequest::postString('header'));
Loading history...
129
            $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

129
            $queue->setDisplayName(/** @scrutinizer ignore-type */ WebRequest::postString('displayName'));
Loading history...
130
            $queue->setHelp(WebRequest::postString('help'));
131
132
            $proceed = true;
133
134
            $foundRequestQueue = RequestQueue::getByDisplayName($database, $queue->getDisplayName(), 1);
135
            if ($foundRequestQueue !== false && $foundRequestQueue->getId() !== $queue->getId()) {
136
                // FIXME: domain
137
                SessionAlert::error("The chosen target display name is already in use. Please choose another.");
138
                $proceed = false;
139
            }
140
141
            $foundRequestQueue = RequestQueue::getByHeader($database, $queue->getHeader(), 1);
142
            if ($foundRequestQueue !== false && $foundRequestQueue->getId() !== $queue->getId()) {
143
                // FIXME: domain
144
                SessionAlert::error("The chosen header is already in use. Please choose another.");
145
                $proceed = false;
146
            }
147
148
            if ($proceed) {
149
                Logger::requestQueueEdited($database, $queue);
150
                $queue->save();
151
                $this->redirect('queueManagement');
152
            }
153
            else {
154
                $this->populateFromObject($queue);
155
156
                $this->assign('createMode', false);
157
                $this->setTemplate('queue-management/edit.tpl');
158
            }
159
        }
160
        else {
161
            $this->populateFromObject($queue);
162
163
            $this->assign('createMode', false);
164
            $this->setTemplate('queue-management/edit.tpl');
165
        }
166
    }
167
168
    /**
169
     * @param RequestQueue $queue
170
     */
171
    protected function populateFromObject(RequestQueue $queue): void
172
    {
173
        $this->assignCSRFToken();
174
175
        $this->assign('header', $queue->getHeader());
176
        $this->assign('displayName', $queue->getDisplayName());
177
        $this->assign('apiName', $queue->getApiName());
178
        $this->assign('enabled', $queue->isEnabled());
179
        $this->assign('default', $queue->isDefault());
180
        $this->assign('antispoof', $queue->isDefaultAntispoof());
181
        $this->assign('titleblacklist', $queue->isDefaultTitleBlacklist());
182
        $this->assign('help', $queue->getHelp());
183
        $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

183
        $this->assign('logName', /** @scrutinizer ignore-deprecated */ $queue->getLogName());
Loading history...
184
        $this->assign('legacyStatus', $queue->getLegacyStatus());
0 ignored issues
show
Deprecated Code introduced by
The function Waca\DataObjects\RequestQueue::getLegacyStatus() 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

184
        $this->assign('legacyStatus', /** @scrutinizer ignore-deprecated */ $queue->getLegacyStatus());
Loading history...
185
    }
186
}