Passed
Pull Request — master (#1066)
by Simon
04:26 queued 26s
created

PageConfirmEmail::main()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 85
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 85
ccs 0
cts 24
cp 0
rs 6.6166
cc 13
nc 24
nop 0
crap 182

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
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Pages\Request;
11
12
use Exception;
13
use Waca\DataObjects\Request;
14
use Waca\DataObjects\RequestData;
15
use Waca\Exceptions\ApplicationLogicException;
16
use Waca\Exceptions\OptimisticLockFailedException;
17
use Waca\Helpers\Logger;
18
use Waca\RequestStatus;
19
use Waca\Tasks\PublicInterfacePageBase;
20
use Waca\WebRequest;
21
22
class PageConfirmEmail extends PublicInterfacePageBase
23
{
24
    /**
25
     * Main function for this page, when no specific actions are called.
26
     * @throws ApplicationLogicException
27
     * @throws Exception
28
     */
29
    protected function main()
30
    {
31
        $id = WebRequest::getInt('id');
32
        $si = WebRequest::getString('si');
33
34
        if ($id === null || $si === null) {
35
            throw new ApplicationLogicException('Link incomplete - please double check the link you received.');
36
        }
37
38
        /** @var Request|false $request */
39
        $request = Request::getById($id, $this->getDatabase());
40
41
        if ($request === false) {
0 ignored issues
show
introduced by
The condition $request === false is always false.
Loading history...
42
            throw new ApplicationLogicException('Request not found');
43
        }
44
45
        if ($request->getEmailConfirm() === 'Confirmed') {
46
            // request has already been confirmed. Bomb out silently.
47
            $this->redirect('requestSubmitted');
48
49
            return;
50
        }
51
52
        if ($request->getEmailConfirm() === $si) {
53
            $request->setEmailConfirm('Confirmed');
54
        }
55
        else {
56
            throw new ApplicationLogicException('The confirmation value does not appear to match the expected value');
57
        }
58
59
        try {
60
            $request->save();
61
        }
62
        catch (OptimisticLockFailedException $ex) {
63
            // Okay. Someone's edited this in the time between us loading this page and doing the checks, and us getting
64
            // to saving the page. We *do not* want to show an optimistic lock failure, the most likely problem is they
65
            // double-loaded this page (see #255). Let's confirm this, and bomb out with a success message if it's the
66
            // case.
67
68
            $request = Request::getById($id, $this->getDatabase());
69
            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

69
            if ($request->/** @scrutinizer ignore-call */ getEmailConfirm() === 'Confirmed') {
Loading history...
70
                // we've already done the sanity checks above
71
72
                $this->redirect('requestSubmitted');
73
74
                // skip the log and notification
75
                return;
76
            }
77
78
            // something really weird happened. Another race condition?
79
            throw $ex;
80
        }
81
82
        Logger::emailConfirmed($this->getDatabase(), $request);
83
84
        if ($request->getStatus() != RequestStatus::CLOSED) {
85
            $this->getNotificationHelper()->requestReceived($request);
86
        }
87
88
        RequestData::saveForRequest($request,
89
            RequestData::TYPE_CONFIRM_USERAGENT, WebRequest::userAgent());
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::userAgent() can also be of type null; however, parameter $value of Waca\DataObjects\RequestData::saveForRequest() 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

89
            RequestData::TYPE_CONFIRM_USERAGENT, /** @scrutinizer ignore-type */ WebRequest::userAgent());
Loading history...
90
91
        $xffProvider = $this->getXffTrustProvider();
92
        $trustedIp = $xffProvider->getTrustedClientIp(WebRequest::remoteAddress(), WebRequest::forwardedAddress());
93
94
        if (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
95
            RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV4, $trustedIp);
96
        }
97
        elseif (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
98
            RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV6, $trustedIp);
99
        }
100
101
        foreach ($this->getSiteConfiguration()->getAcceptClientHints() as $header)
102
        {
103
            $value = WebRequest::httpHeader($header);
104
105
            if ($value === null) {
106
                continue;
107
            }
108
109
            RequestData::saveForRequest($request,
110
                RequestData::TYPE_CONFIRM_CLIENTHINT, $value, $header);
111
        }
112
113
        $this->redirect('requestSubmitted');
114
    }
115
}