Failed Conditions
Pull Request — newinternal (#527)
by Simon
16:02 queued 05:59
created

JobQueue::save()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 0
loc 53
rs 8.7143
c 0
b 0
f 0

How to fix   Long Method   

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\DataObjects;
10
11
use Exception;
12
use Waca\Background\Task\BotCreationTask;
13
use Waca\Background\Task\UserCreationTask;
14
use Waca\Background\Task\WelcomeUserTask;
15
use Waca\DataObject;
16
use Waca\Exceptions\OptimisticLockFailedException;
17
18
class JobQueue extends DataObject
19
{
20
    /*
21
     * Status workflow is this:
22
     *
23
     * 1) Ready. The job has been added to the queue
24
     * 1) Waiting. The job has been picked up by the worker
25
     * 2) Running. The job is actively being processed.
26
     * 3) Complete / Failed. The job has been processed
27
     *
28
     */
29
    const STATUS_READY = 'ready';
30
    const STATUS_WAITING = 'waiting';
31
    const STATUS_RUNNING = 'running';
32
    const STATUS_COMPLETE = 'complete';
33
    const STATUS_CANCELLED = 'cancelled';
34
    const STATUS_FAILED = 'failed';
35
    const STATUS_HELD = 'held';
36
37
    /** @var string */
38
    private $task;
39
    /** @var int */
40
    private $user;
41
    /** @var int */
42
    private $request;
43
    /** @var int */
44
    private $emailtemplate;
45
    /** @var string */
46
    private $status;
47
    /** @var string */
48
    private $enqueue;
49
    /** @var string */
50
    private $parameters;
51
    /** @var string */
52
    private $error;
53
    /** @var int */
54
    private $acknowledged;
55
    /** @var int */
56
    private $parent;
57
58
    /**
59
     * This feels like the least bad place to put this method.
60
     */
61
    public static function getTaskDescriptions() {
62
        return array(
63
            BotCreationTask::class  => 'Create account (via bot)',
64
            UserCreationTask::class => 'Create account (via OAuth)',
65
            WelcomeUserTask::class  => 'Welcome user',
66
        );
67
    }
68
69
    /**
70
     * Saves a data object to the database, either updating or inserting a record.
71
     * @return void
72
     * @throws Exception
73
     * @throws OptimisticLockFailedException
74
     */
75
    public function save()
76
    {
77
        if ($this->isNew()) {
78
            // insert
79
            $statement = $this->dbObject->prepare(<<<SQL
80
                INSERT INTO jobqueue (task, user, request, emailtemplate, parameters, parent) 
81
                VALUES (:task, :user, :request, :emailtemplate, :parameters, :parent)
82
SQL
83
            );
84
            $statement->bindValue(":task", $this->task);
85
            $statement->bindValue(":user", $this->user);
86
            $statement->bindValue(":request", $this->request);
87
            $statement->bindValue(":emailtemplate", $this->emailtemplate);
88
            $statement->bindValue(":parameters", $this->parameters);
89
            $statement->bindValue(":parent", $this->parent);
90
91
            if ($statement->execute()) {
92
                $this->id = (int)$this->dbObject->lastInsertId();
93
            }
94
            else {
95
                throw new Exception($statement->errorInfo());
96
            }
97
        }
98
        else {
99
            // update
100
            $statement = $this->dbObject->prepare(<<<SQL
101
                UPDATE jobqueue SET 
102
                      status = :status
103
                    , error = :error
104
                    , acknowledged = :ack
105
                    , updateversion = updateversion + 1
106
                WHERE id = :id AND updateversion = :updateversion;
107
SQL
108
            );
109
110
            $statement->bindValue(":id", $this->id);
111
            $statement->bindValue(":updateversion", $this->updateversion);
112
113
            $statement->bindValue(":status", $this->status);
114
            $statement->bindValue(":error", $this->error);
115
            $statement->bindValue(":ack", $this->acknowledged);
116
117
            if (!$statement->execute()) {
118
                throw new Exception($statement->errorInfo());
119
            }
120
121
            if ($statement->rowCount() !== 1) {
122
                throw new OptimisticLockFailedException();
123
            }
124
125
            $this->updateversion++;
126
        }
127
    }
128
129
    #region Properties
130
131
    /**
132
     * @return string
133
     */
134
    public function getTask()
135
    {
136
        return $this->task;
137
    }
138
139
    /**
140
     * @param string $task
141
     */
142
    public function setTask($task)
143
    {
144
        $this->task = $task;
145
    }
146
147
    /**
148
     * @return int
149
     */
150
    public function getTriggerUserId()
151
    {
152
        return $this->user;
153
    }
154
155
    /**
156
     * @param int $user
157
     */
158
    public function setTriggerUserId($user)
159
    {
160
        $this->user = $user;
161
    }
162
163
    /**
164
     * @return int
165
     */
166
    public function getRequest()
167
    {
168
        return $this->request;
169
    }
170
171
    /**
172
     * @param int $request
173
     */
174
    public function setRequest($request)
175
    {
176
        $this->request = $request;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getStatus()
183
    {
184
        return $this->status;
185
    }
186
187
    /**
188
     * @param string $status
189
     */
190
    public function setStatus($status)
191
    {
192
        $this->status = $status;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getEnqueue()
199
    {
200
        return $this->enqueue;
201
    }
202
203
    /**
204
     * @param string $enqueue
205
     */
206
    public function setEnqueue($enqueue)
207
    {
208
        $this->enqueue = $enqueue;
209
    }
210
211
    /**
212
     * @return string
213
     */
214
    public function getParameters()
215
    {
216
        return $this->parameters;
217
    }
218
219
    /**
220
     * @param string $parameters
221
     */
222
    public function setParameters($parameters)
223
    {
224
        $this->parameters = $parameters;
225
    }
226
227
    /**
228
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
229
     */
230
    public function getError()
231
    {
232
        return $this->error;
233
    }
234
235
    /**
236
     * @param mixed $error
237
     */
238
    public function setError($error)
239
    {
240
        $this->error = $error;
241
    }
242
243
    /**
244
     * @return int
245
     */
246
    public function getAcknowledged()
247
    {
248
        return $this->acknowledged;
249
    }
250
251
    /**
252
     * @param int $acknowledged
253
     */
254
    public function setAcknowledged($acknowledged)
255
    {
256
        $this->acknowledged = $acknowledged;
257
    }
258
259
    /**
260
     * @return int
261
     */
262
    public function getParent()
263
    {
264
        return $this->parent;
265
    }
266
267
    /**
268
     * @param int $parent
269
     */
270
    public function setParent($parent)
271
    {
272
        $this->parent = $parent;
273
    }
274
275
    /**
276
     * @return int
277
     */
278
    public function getEmailTemplate()
279
    {
280
        return $this->emailtemplate;
281
    }
282
283
    /**
284
     * @param int $emailTemplate
285
     */
286
    public function setEmailTemplate($emailTemplate)
287
    {
288
        $this->emailtemplate = $emailTemplate;
289
    }
290
    #endregion
291
}