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

PageCloseRequest::getTemplate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 10
cc 4
nc 3
nop 1
crap 20
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\RequestAction;
10
11
use Exception;
12
use Waca\DataObjects\EmailTemplate;
13
use Waca\DataObjects\Request;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\ApplicationLogicException;
16
use Waca\Helpers\Logger;
17
use Waca\PdoDatabase;
18
use Waca\SessionAlert;
19
use Waca\WebRequest;
20
21
class PageCloseRequest extends RequestActionBase
22
{
23
    protected function main()
24
    {
25
        $this->processClose();
26
    }
0 ignored issues
show
Coding Style introduced by
Expected //end main()
Loading history...
27
28
    /**
29
     * Main function for this page, when no specific actions are called.
30
     * @throws ApplicationLogicException
31
     */
32
    final protected function processClose()
33
    {
34
        $this->checkPosted();
35
        $database = $this->getDatabase();
36
37
        $currentUser = User::getCurrent($database);
38
        $template = $this->getTemplate($database);
39
        $request = $this->getRequest($database);
40
        $request->setUpdateVersion(WebRequest::postInt('updateversion'));
41
42
        if ($request->getStatus() === 'Closed') {
43
            throw new ApplicationLogicException('Request is already closed');
44
        }
45
46
        if ($this->confirmEmailAlreadySent($request, $template)) {
47
            return;
48
        }
49
50
        if ($this->confirmReserveOverride($request, $template, $currentUser, $database)) {
51
            return;
52
        }
53
54
        if ($this->confirmAccountCreated($request, $template)) {
55
            return;
56
        }
57
58
        // I think we're good here...
59
        $request->setStatus('Closed');
60
        $request->setReserved(null);
61
62
        Logger::closeRequest($database, $request, $template->getId(), null);
63
64
        $request->save();
65
66
        // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and
67
        // be rolled back.
68
69
        $this->getNotificationHelper()->requestClosed($request, $template->getName());
70
        SessionAlert::success("Request {$request->getId()} has been closed");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $request instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
71
72
        $this->sendMail($request, $template->getText(), $currentUser, false);
73
74
        $this->redirect();
75
    }
0 ignored issues
show
Coding Style introduced by
Expected //end processClose()
Loading history...
76
77
    /**
78
     * @param PdoDatabase $database
79
     *
80
     * @return EmailTemplate
81
     * @throws ApplicationLogicException
82
     */
83
    protected function getTemplate(PdoDatabase $database)
84
    {
85
        $templateId = WebRequest::postInt('template');
86
        if ($templateId === null) {
87
            throw new ApplicationLogicException('No template specified');
88
        }
89
90
        /** @var EmailTemplate $template */
91
        $template = EmailTemplate::getById($templateId, $database);
92
        if ($template === false || !$template->getActive()) {
93
            throw new ApplicationLogicException('Invalid or inactive template specified');
94
        }
95
96
        return $template;
97
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getTemplate()
Loading history...
98
99
    /**
100
     * @param Request       $request
101
     * @param EmailTemplate $template
102
     *
103
     * @return bool
104
     */
105
    protected function confirmEmailAlreadySent(Request $request, EmailTemplate $template)
106
    {
107
        if ($this->checkEmailAlreadySent($request)) {
108
            $this->showConfirmation($request, $template, 'close-confirmations/email-sent.tpl');
109
110
            return true;
111
        }
112
113
        return false;
114
    }
0 ignored issues
show
Coding Style introduced by
Expected //end confirmEmailAlreadySent()
Loading history...
115
116
    protected function checkEmailAlreadySent(Request $request)
117
    {
118
        if ($request->getEmailSent() && !WebRequest::postBoolean('emailSentOverride')) {
119
            return true;
120
        }
121
122
        return false;
123
    }
0 ignored issues
show
Coding Style introduced by
Expected //end checkEmailAlreadySent()
Loading history...
124
125
    protected function checkReserveOverride(Request $request, User $currentUser)
126
    {
127
        $reservationId = $request->getReserved();
128
129
        if ($reservationId !== 0 && $reservationId !== null) {
130
            if (!WebRequest::postBoolean('reserveOverride')) {
131
                if ($currentUser->getId() !== $reservationId) {
132
                    return true;
133
                }
134
            }
135
        }
136
137
        return false;
138
    }
0 ignored issues
show
Coding Style introduced by
Expected //end checkReserveOverride()
Loading history...
139
140
    /**
141
     * @param Request       $request
142
     * @param EmailTemplate $template
143
     * @param User          $currentUser
144
     * @param PdoDatabase   $database
145
     *
146
     * @return bool
147
     */
148
    protected function confirmReserveOverride(
149
        Request $request,
150
        EmailTemplate $template,
151
        User $currentUser,
152
        PdoDatabase $database
153
    ) {
154
        if ($this->checkReserveOverride($request, $currentUser)) {
155
            $this->assign('reserveUser', User::getById($request->getReserved(), $database)->getUsername());
156
            $this->showConfirmation($request, $template, 'close-confirmations/reserve-override.tpl');
157
158
            return true;
159
        }
160
161
        return false;
162
    }
0 ignored issues
show
Coding Style introduced by
Expected //end confirmReserveOverride()
Loading history...
163
164
    /**
165
     * @param Request       $request
166
     * @param EmailTemplate $template
167
     *
168
     * @return bool
169
     * @throws \Waca\Exceptions\CurlException
170
     */
171
    protected function confirmAccountCreated(Request $request, EmailTemplate $template)
172
    {
173
        if ($this->checkAccountCreated($request, $template)) {
174
            $this->showConfirmation($request, $template, 'close-confirmations/account-created.tpl');
175
176
            return true;
177
        }
178
179
        return false;
180
    }
0 ignored issues
show
Coding Style introduced by
Expected //end confirmAccountCreated()
Loading history...
181
182
    protected function checkAccountCreated(Request $request, EmailTemplate $template)
183
    {
184
        if ($template->getDefaultAction() === EmailTemplate::CREATED && !WebRequest::postBoolean('createOverride')) {
185
            $parameters = array(
186
                'action'  => 'query',
187
                'list'    => 'users',
188
                'format'  => 'php',
189
                'ususers' => $request->getName(),
190
            );
191
192
            $content = $this->getHttpHelper()->get($this->getSiteConfiguration()->getMediawikiWebServiceEndpoint(),
193
                $parameters);
194
195
            $apiResult = unserialize($content);
196
            $exists = !isset($apiResult['query']['users']['0']['missing']);
197
198
            if (!$exists) {
199
                return true;
200
            }
201
        }
202
203
        return false;
204
    }
0 ignored issues
show
Coding Style introduced by
Expected //end checkAccountCreated()
Loading history...
205
206
    /**
207
     * @param Request $request
208
     * @param string  $mailText
209
     * @param User    $currentUser
210
     * @param boolean $ccMailingList
211
     */
212
    protected function sendMail(Request $request, $mailText, User $currentUser, $ccMailingList)
213
    {
214
        $headers = array(
215
            'X-ACC-Request' => $request->getId(),
216
            'X-ACC-UserID'  => $currentUser->getId(),
217
        );
218
219
        if ($ccMailingList) {
220
            $headers['Cc'] = '[email protected]';
221
        }
222
223
        $helper = $this->getEmailHelper();
224
225
        $emailSig = $currentUser->getEmailSig();
226
        if ($emailSig !== '' || $emailSig !== null) {
0 ignored issues
show
introduced by
The condition $emailSig !== null is always true.
Loading history...
227
            $emailSig = "\n\n" . $emailSig;
228
        }
229
230
        $subject = "RE: [ACC #{$request->getId()}] English Wikipedia Account Request";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $request instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
231
        $content = $mailText . $emailSig;
232
233
        $helper->sendMail($request->getEmail(), $subject, $content, $headers);
234
235
        $request->setEmailSent(true);
236
    }
0 ignored issues
show
Coding Style introduced by
Expected //end sendMail()
Loading history...
237
238
    /**
239
     * @param Request       $request
240
     * @param EmailTemplate $template
241
     * @param string        $templateName
242
     *
243
     * @throws Exception
244
     * @return void
245
     */
246
    protected function showConfirmation(Request $request, EmailTemplate $template, $templateName)
247
    {
248
        $this->assignCSRFToken();
249
250
        $this->assign('request', $request->getId());
251
        $this->assign('template', $template->getId());
252
253
        $this->assign('updateversion', $request->getUpdateVersion());
254
255
        $this->assign('emailSentOverride', WebRequest::postBoolean('emailSentOverride') ? 'true' : 'false');
256
        $this->assign('reserveOverride', WebRequest::postBoolean('reserveOverride') ? 'true' : 'false');
257
        $this->assign('createOverride', WebRequest::postBoolean('createOverride') ? 'true' : 'false');
258
259
        $this->setTemplate($templateName);
260
    }
0 ignored issues
show
Coding Style introduced by
Expected //end showConfirmation()
Loading history...
261
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
262