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

PageConfirmEmail   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 26
c 2
b 0
f 0
dl 0
loc 64
ccs 0
cts 35
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B main() 0 57 8
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\Request;
10
11
use Exception;
12
use Waca\DataObjects\Request;
13
use Waca\Exceptions\ApplicationLogicException;
14
use Waca\Exceptions\OptimisticLockFailedException;
15
use Waca\Helpers\Logger;
16
use Waca\Tasks\PublicInterfacePageBase;
17
use Waca\WebRequest;
18
19
class PageConfirmEmail extends PublicInterfacePageBase
20
{
21
    /**
22
     * Main function for this page, when no specific actions are called.
23
     * @throws ApplicationLogicException
24
     * @throws Exception
25
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag for "OptimisticLockFailedException" exception
Loading history...
26
    protected function main()
27
    {
28
        $id = WebRequest::getInt('id');
29
        $si = WebRequest::getString('si');
30
31
        if ($id === null || $si === null) {
32
            throw new ApplicationLogicException('Link incomplete - please double check the link you received.');
33
        }
34
35
        /** @var Request|false $request */
36
        $request = Request::getById($id, $this->getDatabase());
37
38
        if ($request === false) {
0 ignored issues
show
introduced by
The condition $request === false is always false.
Loading history...
39
            throw new ApplicationLogicException('Request not found');
40
        }
41
42
        if ($request->getEmailConfirm() === 'Confirmed') {
43
            // request has already been confirmed. Bomb out silently.
44
            $this->redirect('requestSubmitted');
45
46
            return;
47
        }
48
49
        if ($request->getEmailConfirm() === $si) {
50
            $request->setEmailConfirm('Confirmed');
51
        }
52
        else {
53
            throw new ApplicationLogicException('The confirmation value does not appear to match the expected value');
54
        }
55
56
        try {
57
            $request->save();
58
        }
59
        catch (OptimisticLockFailedException $ex) {
60
            // Okay. Someone's edited this in the time between us loading this page and doing the checks, and us getting
61
            // to saving the page. We *do not* want to show an optimistic lock failure, the most likely problem is they
62
            // double-loaded this page (see #255). Let's confirm this, and bomb out with a success message if it's the
63
            // case.
64
65
            $request = Request::getById($id, $this->getDatabase());
66
            if ($request->getEmailConfirm() === 'Confirmed') {
0 ignored issues
show
Bug introduced by
The method getEmailConfirm() does not exist on Waca\DataObject. It seems like you code against a sub-type of Waca\DataObject such as Waca\DataObjects\Request. ( Ignorable by Annotation )

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

66
            if ($request->/** @scrutinizer ignore-call */ getEmailConfirm() === 'Confirmed') {
Loading history...
67
                // we've already done the sanity checks above
68
69
                $this->redirect('requestSubmitted');
70
71
                // skip the log and notification
72
                return;
73
            }
74
75
            // something really weird happened. Another race condition?
76
            throw $ex;
77
        }
78
79
        Logger::emailConfirmed($this->getDatabase(), $request);
80
        $this->getNotificationHelper()->requestReceived($request);
81
82
        $this->redirect('requestSubmitted');
83
    }
0 ignored issues
show
Coding Style introduced by
Expected //end main()
Loading history...
84
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...