Passed
Push — exception-fixes ( 209cd0...a6f08f )
by Simon
06:49 queued 03:22
created

CreationTaskBase::execute()   C

Complexity

Conditions 12
Paths 63

Size

Total Lines 68
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 68
ccs 0
cts 50
cp 0
rs 6.9666
cc 12
nc 63
nop 0
crap 156

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\Background;
10
11
use Exception;
12
use Waca\DataObjects\Request;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\ApplicationLogicException;
15
use Waca\Helpers\Interfaces\IMediaWikiClient;
16
use Waca\Helpers\Logger;
17
use Waca\Helpers\MediaWikiHelper;
18
use Waca\Helpers\RequestEmailHelper;
19
use Waca\RequestStatus;
20
21
abstract class CreationTaskBase extends BackgroundTaskBase
22
{
23
    /** @var Request */
24
    private $request;
25
    /**
26
     * @var MediaWikiHelper
27
     * Don't use this directly.
28
     */
29
    private $mwHelper = null;
30
31
    public function execute()
32
    {
33
        $this->request = $this->getRequest();
34
        $user = $this->getTriggerUser();
35
        $parameters = $this->getParameters();
36
37
        if ($this->request->getStatus() !== RequestStatus::JOBQUEUE) {
38
            $this->markCancelled('Request is not deferred to the job queue');
39
40
            return;
41
        }
42
43
        if ($this->request->getEmailSent() != 0 && !isset($parameters->emailText)) {
44
            $this->markFailed('Request has already been sent a templated email');
45
46
            return;
47
        }
48
49
        if ($this->request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) {
50
            $this->markFailed('Private data of request has been purged.');
51
52
            return;
53
        }
54
55
        $emailText = null;
56
        $ccMailingList = null;
57
        $logTarget = null;
58
59
        if (isset($parameters->emailText) && isset($parameters->ccMailingList)) {
60
            $emailText = $parameters->emailText;
61
            $ccMailingList = $parameters->ccMailingList;
62
            $logTarget = "custom-y";
63
        }
64
65
        if ($this->getEmailTemplate() !== null) {
66
            $emailText = $this->getEmailTemplate()->getText();
67
            $ccMailingList = false;
68
            $logTarget = $this->getEmailTemplate()->getId();
69
        }
70
71
        if ($emailText === null || $ccMailingList === null) {
72
            $this->markFailed('Unable to get closure email text');
73
74
            return;
75
        }
76
77
        try {
78
            $this->performCreation($user);
79
80
            $this->request->setStatus(RequestStatus::CLOSED);
81
            $this->request->setReserved(null);
82
            $this->request->setEmailSent(true);
83
            $this->request->save();
84
85
            // Log the closure as the user
86
            $logComment = $this->getEmailTemplate() === null ? $emailText : null;
87
            Logger::closeRequest($this->getDatabase(), $this->request, $logTarget, $logComment, $this->getTriggerUser());
0 ignored issues
show
Bug introduced by
It seems like $logTarget can also be of type string; however, parameter $target of Waca\Helpers\Logger::closeRequest() does only seem to accept integer, 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

87
            Logger::closeRequest($this->getDatabase(), $this->request, /** @scrutinizer ignore-type */ $logTarget, $logComment, $this->getTriggerUser());
Loading history...
88
89
            $requestEmailHelper = new RequestEmailHelper($this->getEmailHelper());
90
            $requestEmailHelper->sendMail($this->request, $emailText, $this->getTriggerUser(), $ccMailingList);
91
        }
92
        catch (Exception $ex) {
93
            $this->markFailed($ex->getMessage());
94
95
            return;
96
        }
97
98
        $this->markComplete();
99
    }
100
101
    /**
102
     * @return IMediaWikiClient
103
     */
104
    protected abstract function getMediaWikiClient();
105
106
    protected function getMediaWikiHelper()
107
    {
108
        if ($this->mwHelper === null) {
109
            $this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration());
110
        }
111
112
        return $this->mwHelper;
113
    }
114
115
    protected function getCreationReason(Request $request, User $user)
116
    {
117
        return 'Requested account at [[WP:ACC]], request #' . $request->getId();
118
    }
119
120
    /**
121
     * @param string $name
122
     *
123
     * @return bool
124
     */
125
    protected function checkAccountExists($name)
126
    {
127
        return $this->getMediaWikiHelper()->checkAccountExists($name);
128
    }
129
130
    protected function markFailed($reason = null)
131
    {
132
        $this->request->setStatus(RequestStatus::HOSPITAL);
133
        $this->request->save();
134
135
        $this->getNotificationHelper()->requestCreationFailed($this->request, $this->getTriggerUser());
136
137
        Logger::hospitalised($this->getDatabase(), $this->request);
138
139
        parent::markFailed($reason);
140
    }
141
142
    /**
143
     * @param $user
144
     *
145
     * @throws ApplicationLogicException
146
     */
147
    protected function performCreation($user)
148
    {
149
        $mw = $this->getMediaWikiHelper();
150
151
        $reason = $this->getCreationReason($this->request, $user);
152
153
        if ($this->checkAccountExists($this->request->getName())) {
154
            throw new ApplicationLogicException('Account already exists');
155
        }
156
157
        $mw->createAccount($this->request->getName(), $this->request->getEmail(), $reason);
158
159
        if (!$this->checkAccountExists($this->request->getName())) {
160
            throw new ApplicationLogicException('Account creation appeared to succeed but account does not exist.');
161
        }
162
163
        $this->request->setStatus(RequestStatus::CLOSED);
164
        $this->request->save();
165
    }
166
}