Passed
Push — search ( bdb480...5e7f6e )
by Simon
17:14 queued 07:17
created

RequestQueue::getEnabledQueues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 PDO;
13
use Waca\DataObject;
14
use Waca\Exceptions\ApplicationLogicException;
15
use Waca\Exceptions\OptimisticLockFailedException;
16
use Waca\PdoDatabase;
17
18
class RequestQueue extends DataObject
19
{
20
    const DEFAULT_DEFAULT = 'default';
21
    const DEFAULT_ANTISPOOF = 'antispoof';
22
    const DEFAULT_TITLEBLACKLIST = 'titleblacklist';
23
24
    /** @var int */
25
    private $enabled = 0;
26
    /** @var int */
27
    private $isdefault = 0;
28
    /** @var int */
29
    private $defaultantispoof = 0;
30
    /** @var int */
31
    private $defaulttitleblacklist = 0;
32
    /** @var int */
33
    private $domain;
34
    /** @var string */
35
    private $apiname;
36
    /** @var string */
37
    private $displayname;
38
    /** @var string */
39
    private $header;
40
    /** @var string|null */
41
    private $help;
42
    /**
43
     * @var string
44
     * @deprecated Removal due as part of #607
45
     */
46
    private $logname;
47
    /**
48
     * @param PdoDatabase $database
49
     *
50
     * @return RequestQueue[]
51
     */
52
    public static function getAllQueues(PdoDatabase $database)
53
    {
54
        $statement = $database->prepare(<<<SQL
55
            SELECT * FROM requestqueue;
56
SQL
57
        );
58
        $statement->execute();
59
60
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
61
62
        /** @var RequestQueue $t */
63
        foreach ($resultObject as $t) {
64
            $t->setDatabase($database);
65
        }
66
67
        return $resultObject;
68
    }
69
70
    /**
71
     * @param PdoDatabase $database
72
     *
73
     * @return RequestQueue[]
74
     */
75
    public static function getEnabledQueues(PdoDatabase $database)
76
    {
77
        $statement = $database->prepare(<<<SQL
78
            SELECT * FROM requestqueue WHERE enabled = 1;
79
SQL
80
        );
81
        $statement->execute();
82
83
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
84
85
        /** @var RequestQueue $t */
86
        foreach ($resultObject as $t) {
87
            $t->setDatabase($database);
88
        }
89
90
        return $resultObject;
91
    }
92
93
    /**
94
     * @param PdoDatabase $database
95
     * @param string      $apiName
96
     * @param int         $domain
97
     *
98
     * @return false|RequestQueue
99
     */
100
    public static function getByApiName(PdoDatabase $database, string $apiName, int $domain)
101
    {
102
        $statement = $database->prepare(<<<SQL
103
            SELECT * FROM requestqueue WHERE apiname = :apiName AND domain = :domain;
104
SQL
105
        );
106
107
        $statement->execute([
108
            ':apiName' => $apiName,
109
            ':domain'  => $domain,
110
        ]);
111
112
        /** @var RequestQueue|false $result */
113
        $result = $statement->fetchObject(get_called_class());
114
115
        if ($result !== false) {
116
            $result->setDatabase($database);
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * @param PdoDatabase $database
124
     * @param string      $displayName
125
     * @param int         $domain
126
     *
127
     * @return false|RequestQueue
128
     */
129
    public static function getByDisplayName(PdoDatabase $database, string $displayName, int $domain)
130
    {
131
        $statement = $database->prepare(<<<SQL
132
            SELECT * FROM requestqueue WHERE displayname = :displayName AND domain = :domain;
133
SQL
134
        );
135
136
        $statement->execute([
137
            ':displayName' => $displayName,
138
            ':domain'      => $domain,
139
        ]);
140
141
        /** @var RequestQueue|false $result */
142
        $result = $statement->fetchObject(get_called_class());
143
144
        if ($result !== false) {
145
            $result->setDatabase($database);
146
        }
147
148
        return $result;
149
    }
150
151
    /**
152
     * @param PdoDatabase $database
153
     * @param string      $header
154
     * @param int         $domain
155
     *
156
     * @return false|RequestQueue
157
     */
158
    public static function getByHeader(PdoDatabase $database, string $header, int $domain)
159
    {
160
        $statement = $database->prepare(<<<SQL
161
            SELECT * FROM requestqueue WHERE header = :header AND domain = :domain;
162
SQL
163
        );
164
165
        $statement->execute([
166
            ':header' => $header,
167
            ':domain' => $domain,
168
        ]);
169
170
        /** @var RequestQueue|false $result */
171
        $result = $statement->fetchObject(get_called_class());
172
173
        if ($result !== false) {
174
            $result->setDatabase($database);
175
        }
176
177
        return $result;
178
    }
179
180
    /**
181
     * @param PdoDatabase $database
182
     * @param int         $domain
183
     *
184
     * @param string      $defaultType The type of default queue to get.
185
     *
186
     * @return false|RequestQueue
187
     * @throws ApplicationLogicException
188
     */
189
    public static function getDefaultQueue(PdoDatabase $database, int $domain, string $defaultType = 'default')
190
    {
191
        switch ($defaultType) {
192
            case self::DEFAULT_DEFAULT:
193
                $sql = 'SELECT * FROM requestqueue WHERE isdefault = 1 AND domain = :domain;';
194
                break;
195
            case self::DEFAULT_ANTISPOOF:
196
                $sql = 'SELECT * FROM requestqueue WHERE defaultantispoof = 1 AND domain = :domain;';
197
                break;
198
            case self::DEFAULT_TITLEBLACKLIST:
199
                $sql = 'SELECT * FROM requestqueue WHERE defaulttitleblacklist = 1 AND domain = :domain;';
200
                break;
201
            default:
202
                throw new ApplicationLogicException('Unknown request for default queue');
203
        }
204
205
        $statement = $database->prepare($sql);
206
207
        $statement->execute([':domain' => $domain]);
208
209
        /** @var RequestQueue|false $result */
210
        $result = $statement->fetchObject(get_called_class());
211
212
        if ($result !== false) {
213
            $result->setDatabase($database);
214
        }
215
216
        return $result;
217
    }
218
219
    public function save()
220
    {
221
        // find and squish existing defaults
222
        if ($this->isDefault()) {
223
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET isdefault = 0 WHERE isdefault = 1 AND domain = :domain;');
224
            $squishStatement->execute([':domain' => $this->domain]);
225
        }
226
227
        if ($this->isDefaultAntispoof()) {
228
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET defaultantispoof = 0 WHERE defaultantispoof = 1 AND domain = :domain;');
229
            $squishStatement->execute([':domain' => $this->domain]);
230
        }
231
232
        if ($this->isDefaultTitleBlacklist()) {
233
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET defaulttitleblacklist = 0 WHERE defaulttitleblacklist = 1 AND domain = :domain;');
234
            $squishStatement->execute([':domain' => $this->domain]);
235
        }
236
237
        if ($this->isNew()) {
238
            // insert
239
            $statement = $this->dbObject->prepare(<<<SQL
240
                INSERT INTO requestqueue (
241
                    enabled, isdefault, defaultantispoof, defaulttitleblacklist, domain, apiname, displayname, header, help, logname
242
                ) VALUES (
243
                    :enabled, :isdefault, :defaultantispoof, :defaulttitleblacklist, :domain, :apiname, :displayname, :header, :help, :logname
244
                );
245
SQL
246
            );
247
248
            $statement->bindValue(":enabled", $this->enabled);
249
            $statement->bindValue(":isdefault", $this->isdefault);
250
            $statement->bindValue(":defaultantispoof", $this->defaultantispoof);
251
            $statement->bindValue(":defaulttitleblacklist", $this->defaulttitleblacklist);
252
            $statement->bindValue(":domain", $this->domain);
253
            $statement->bindValue(":apiname", $this->apiname);
254
            $statement->bindValue(":displayname", $this->displayname);
255
            $statement->bindValue(":header", $this->header);
256
            $statement->bindValue(":help", $this->help);
257
            $statement->bindValue(":logname", $this->logname);
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

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

257
            $statement->bindValue(":logname", /** @scrutinizer ignore-deprecated */ $this->logname);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
258
259
            if ($statement->execute()) {
260
                $this->id = (int)$this->dbObject->lastInsertId();
261
            }
262
            else {
263
                throw new Exception($statement->errorInfo());
0 ignored issues
show
Bug introduced by
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

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

263
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
264
            }
265
        }
266
        else {
267
            $statement = $this->dbObject->prepare(<<<SQL
268
                UPDATE requestqueue SET
269
                    enabled = :enabled,
270
                    isdefault = :isdefault,
271
                    defaultantispoof = :defaultantispoof,
272
                    defaulttitleblacklist = :defaulttitleblacklist,
273
                    domain = :domain,
274
                    apiname = :apiname,
275
                    displayname = :displayname,
276
                    header = :header,
277
                    help = :help,
278
                    logname = :logname,
279
                
280
                    updateversion = updateversion + 1
281
				WHERE id = :id AND updateversion = :updateversion;
282
SQL
283
            );
284
285
            $statement->bindValue(":enabled", $this->enabled);
286
            $statement->bindValue(":isdefault", $this->isdefault);
287
            $statement->bindValue(":defaultantispoof", $this->defaultantispoof);
288
            $statement->bindValue(":defaulttitleblacklist", $this->defaulttitleblacklist);
289
            $statement->bindValue(":domain", $this->domain);
290
            $statement->bindValue(":apiname", $this->apiname);
291
            $statement->bindValue(":displayname", $this->displayname);
292
            $statement->bindValue(":header", $this->header);
293
            $statement->bindValue(":help", $this->help);
294
            $statement->bindValue(":logname", $this->logname);
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

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

294
            $statement->bindValue(":logname", /** @scrutinizer ignore-deprecated */ $this->logname);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
295
296
            $statement->bindValue(':id', $this->id);
297
            $statement->bindValue(':updateversion', $this->updateversion);
298
299
            if (!$statement->execute()) {
300
                throw new Exception($statement->errorInfo());
301
            }
302
303
            if ($statement->rowCount() !== 1) {
304
                throw new OptimisticLockFailedException();
305
            }
306
307
            $this->updateversion++;
308
        }
309
    }
310
311
    /**
312
     * @return bool
313
     */
314
    public function isEnabled(): bool
315
    {
316
        return $this->enabled == 1;
317
    }
318
319
    /**
320
     * @param bool $enabled
321
     */
322
    public function setEnabled(bool $enabled): void
323
    {
324
        $this->enabled = $enabled ? 1 : 0;
325
    }
326
327
    /**
328
     * @return bool
329
     */
330
    public function isDefault(): bool
331
    {
332
        return $this->isdefault == 1;
333
    }
334
335
    /**
336
     * @param bool $isDefault
337
     */
338
    public function setDefault(bool $isDefault): void
339
    {
340
        $this->isdefault = $isDefault ? 1 : 0;
341
    }
342
343
    /**
344
     * @return bool
345
     */
346
    public function isDefaultAntispoof(): bool
347
    {
348
        return $this->defaultantispoof == 1;
349
    }
350
351
    /**
352
     * @param bool $isDefault
353
     */
354
    public function setDefaultAntispoof(bool $isDefault): void
355
    {
356
        $this->defaultantispoof = $isDefault ? 1 : 0;
357
    }
358
359
    /**
360
     * @return bool
361
     */
362
    public function isDefaultTitleBlacklist(): bool
363
    {
364
        return $this->defaulttitleblacklist == 1;
365
    }
366
367
    /**
368
     * @param bool $isDefault
369
     */
370
    public function setDefaultTitleBlacklist(bool $isDefault): void
371
    {
372
        $this->defaulttitleblacklist = $isDefault ? 1 : 0;
373
    }
374
375
    /**
376
     * @return int
377
     */
378
    public function getDomain(): int
379
    {
380
        return $this->domain;
381
    }
382
383
    /**
384
     * @param int $domain
385
     */
386
    public function setDomain(int $domain): void
387
    {
388
        $this->domain = $domain;
389
    }
390
391
    /**
392
     * @return string
393
     */
394
    public function getApiName(): string
395
    {
396
        return $this->apiname;
397
    }
398
399
    /**
400
     * @param string $apiName
401
     */
402
    public function setApiName(string $apiName): void
403
    {
404
        $this->apiname = $apiName;
405
    }
406
407
    /**
408
     * @return string
409
     */
410
    public function getDisplayName(): string
411
    {
412
        return $this->displayname;
413
    }
414
415
    /**
416
     * @param string $displayName
417
     */
418
    public function setDisplayName(string $displayName): void
419
    {
420
        $this->displayname = $displayName;
421
    }
422
423
    /**
424
     * @return string
425
     */
426
    public function getHeader(): string
427
    {
428
        return $this->header;
429
    }
430
431
    /**
432
     * @param string $header
433
     */
434
    public function setHeader(string $header): void
435
    {
436
        $this->header = $header;
437
    }
438
439
    /**
440
     * @return string|null
441
     */
442
    public function getHelp(): ?string
443
    {
444
        return $this->help;
445
    }
446
447
    /**
448
     * @param string|null $help
449
     */
450
    public function setHelp(?string $help): void
451
    {
452
        $this->help = $help;
453
    }
454
455
    /**
456
     * @return string
457
     * @deprecated
458
     */
459
    public function getLogName(): string
460
    {
461
        return $this->logname;
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

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

461
        return /** @scrutinizer ignore-deprecated */ $this->logname;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
462
    }
463
464
    /**
465
     * @param string $logName
466
     *
467
     * @deprecated
468
     */
469
    public function setLogName(string $logName): void
470
    {
471
        $this->logname = $logName;
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

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

471
        /** @scrutinizer ignore-deprecated */ $this->logname = $logName;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
472
    }
473
}