Passed
Pull Request — multiproject/db (#703)
by Simon
02:48
created

RequestQueue::setDisplayName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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\OptimisticLockFailedException;
15
use Waca\PdoDatabase;
16
17
class RequestQueue extends DataObject
18
{
19
    /** @var int */
20
    private $enabled = 0;
21
    /** @var int */
22
    private $isdefault = 0;
23
    /** @var int */
24
    private $defaultantispoof = 0;
25
    /** @var int */
26
    private $defaulttitleblacklist = 0;
27
    /** @var int */
28
    private $domain;
29
    /** @var string */
30
    private $apiname;
31
    /** @var string */
32
    private $displayname;
33
    /** @var string */
34
    private $header;
35
    /** @var string|null */
36
    private $help;
37
    /**
38
     * @var string
39
     * @deprecated Removal due as part of #607
40
     */
41
    private $logname;
42
    /**
43
     * @var string
44
     * @deprecated Removal due as part of #602
45
     */
46
    private $legacystatus;
47
48
    /**
49
     * @param PdoDatabase $database
50
     *
51
     * @return RequestQueue[]
52
     */
53
    public static function getAllQueues(PdoDatabase $database)
54
    {
55
        $statement = $database->prepare(<<<SQL
56
            SELECT * FROM requestqueue;
57
SQL
58
        );
59
        $statement->execute();
60
61
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
62
63
        /** @var RequestQueue $t */
64
        foreach ($resultObject as $t) {
65
            $t->setDatabase($database);
66
        }
67
68
        return $resultObject;
69
    }
70
71
    /**
72
     * @param PdoDatabase $database
73
     * @param string      $apiName
74
     * @param int         $domain
75
     *
76
     * @return false|RequestQueue
77
     */
78
    public static function getByApiName(PdoDatabase $database, string $apiName, int $domain)
79
    {
80
        $statement = $database->prepare(<<<SQL
81
            SELECT * FROM requestqueue WHERE apiname = :apiName AND domain = :domain;
82
SQL
83
        );
84
85
        $statement->execute([
86
            ':apiName' => $apiName,
87
            ':domain'  => $domain,
88
        ]);
89
90
        /** @var RequestQueue|false $result */
91
        $result = $statement->fetchObject(get_called_class());
92
93
        if ($result !== false) {
94
            $result->setDatabase($database);
95
        }
96
97
        return $result;
98
    }
99
100
    /**
101
     * @param PdoDatabase $database
102
     * @param string      $displayName
103
     * @param int         $domain
104
     *
105
     * @return false|RequestQueue
106
     */
107
    public static function getByDisplayName(PdoDatabase $database, string $displayName, int $domain)
108
    {
109
        $statement = $database->prepare(<<<SQL
110
            SELECT * FROM requestqueue WHERE displayname = :displayName AND domain = :domain;
111
SQL
112
        );
113
114
        $statement->execute([
115
            ':displayName' => $displayName,
116
            ':domain'      => $domain,
117
        ]);
118
119
        /** @var RequestQueue|false $result */
120
        $result = $statement->fetchObject(get_called_class());
121
122
        if ($result !== false) {
123
            $result->setDatabase($database);
124
        }
125
126
        return $result;
127
    }
128
129
    /**
130
     * @param PdoDatabase $database
131
     * @param string      $header
132
     * @param int         $domain
133
     *
134
     * @return false|RequestQueue
135
     */
136
    public static function getByHeader(PdoDatabase $database, string $header, int $domain)
137
    {
138
        $statement = $database->prepare(<<<SQL
139
            SELECT * FROM requestqueue WHERE header = :header AND domain = :domain;
140
SQL
141
        );
142
143
        $statement->execute([
144
            ':header' => $header,
145
            ':domain' => $domain,
146
        ]);
147
148
        /** @var RequestQueue|false $result */
149
        $result = $statement->fetchObject(get_called_class());
150
151
        if ($result !== false) {
152
            $result->setDatabase($database);
153
        }
154
155
        return $result;
156
    }
157
158
    public function save()
159
    {
160
        // find and squish existing defaults
161
        if ($this->isDefault()) {
162
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET isdefault = 0 WHERE isdefault = 1 AND domain = :domain;');
163
            $squishStatement->execute([':domain' => $this->domain]);
164
        }
165
166
        if ($this->isDefaultAntispoof()) {
167
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET defaultantispoof = 0 WHERE defaultantispoof = 1 AND domain = :domain;');
168
            $squishStatement->execute([':domain' => $this->domain]);
169
        }
170
171
        if ($this->isDefaultTitleBlacklist()) {
172
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET defaulttitleblacklist = 0 WHERE defaulttitleblacklist = 1 AND domain = :domain;');
173
            $squishStatement->execute([':domain' => $this->domain]);
174
        }
175
176
        if ($this->isNew()) {
177
            // insert
178
            $statement = $this->dbObject->prepare(<<<SQL
179
                INSERT INTO requestqueue (
180
                    enabled, isdefault, defaultantispoof, defaulttitleblacklist, domain, apiname, displayname, header, help, logname, legacystatus
181
                ) VALUES (
182
                    :enabled, :isdefault, :defaultantispoof, :defaulttitleblacklist, :domain, :apiname, :displayname, :header, :help, :logname, :legacystatus
183
                );
184
SQL
185
            );
186
187
            $statement->bindValue(":enabled", $this->enabled);
188
            $statement->bindValue(":isdefault", $this->isdefault);
189
            $statement->bindValue(":defaultantispoof", $this->defaultantispoof);
190
            $statement->bindValue(":defaulttitleblacklist", $this->defaulttitleblacklist);
191
            $statement->bindValue(":domain", $this->domain);
192
            $statement->bindValue(":apiname", $this->apiname);
193
            $statement->bindValue(":displayname", $this->displayname);
194
            $statement->bindValue(":header", $this->header);
195
            $statement->bindValue(":help", $this->help);
196
            $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

196
            $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...
197
            $statement->bindValue(":legacystatus", $this->legacystatus);
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$legacystatus has been deprecated: Removal due as part of #602 ( Ignorable by Annotation )

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

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

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...
198
199
            if ($statement->execute()) {
200
                $this->id = (int)$this->dbObject->lastInsertId();
201
            }
202
            else {
203
                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

203
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
204
            }
205
        }
206
        else {
207
            $statement = $this->dbObject->prepare(<<<SQL
208
                UPDATE requestqueue SET
209
                    enabled = :enabled,
210
                    isdefault = :isdefault,
211
                    defaultantispoof = :defaultantispoof,
212
                    defaulttitleblacklist = :defaulttitleblacklist,
213
                    domain = :domain,
214
                    apiname = :apiname,
215
                    displayname = :displayname,
216
                    header = :header,
217
                    help = :help,
218
                    logname = :logname,
219
                    legacystatus = :legacystatus,
220
                
221
                    updateversion = updateversion + 1
222
				WHERE id = :id AND updateversion = :updateversion;
223
SQL
224
            );
225
226
            $statement->bindValue(":enabled", $this->enabled);
227
            $statement->bindValue(":isdefault", $this->isdefault);
228
            $statement->bindValue(":defaultantispoof", $this->defaultantispoof);
229
            $statement->bindValue(":defaulttitleblacklist", $this->defaulttitleblacklist);
230
            $statement->bindValue(":domain", $this->domain);
231
            $statement->bindValue(":apiname", $this->apiname);
232
            $statement->bindValue(":displayname", $this->displayname);
233
            $statement->bindValue(":header", $this->header);
234
            $statement->bindValue(":help", $this->help);
235
            $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

235
            $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...
236
            $statement->bindValue(":legacystatus", $this->legacystatus);
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$legacystatus has been deprecated: Removal due as part of #602 ( Ignorable by Annotation )

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

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

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...
237
238
            $statement->bindValue(':id', $this->id);
239
            $statement->bindValue(':updateversion', $this->updateversion);
240
241
            if (!$statement->execute()) {
242
                throw new Exception($statement->errorInfo());
243
            }
244
245
            if ($statement->rowCount() !== 1) {
246
                throw new OptimisticLockFailedException();
247
            }
248
249
            $this->updateversion++;
250
        }
251
    }
252
253
    /**
254
     * @return bool
255
     */
256 256
    public function isEnabled(): bool
257
    {
258 256
        return $this->enabled == 1;
259
    }
260
261
    /**
262
     * @param bool $enabled
263
     */
264 256
    public function setEnabled(bool $enabled): void
265
    {
266 256
        $this->enabled = $enabled ? 1 : 0;
267 256
    }
268
269
    /**
270
     * @return bool
271
     */
272 256
    public function isDefault(): bool
273
    {
274 256
        return $this->isdefault == 1;
275
    }
276
277
    /**
278
     * @param bool $isDefault
279
     */
280 256
    public function setDefault(bool $isDefault): void
281
    {
282 256
        $this->isdefault = $isDefault ? 1 : 0;
283 256
    }
284
285
    /**
286
     * @return bool
287
     */
288 256
    public function isDefaultAntispoof(): bool
289
    {
290 256
        return $this->defaultantispoof == 1;
291
    }
292
293
    /**
294
     * @param bool $isDefault
295
     */
296 256
    public function setDefaultAntispoof(bool $isDefault): void
297
    {
298 256
        $this->defaultantispoof = $isDefault ? 1 : 0;
299 256
    }
300
301
    /**
302
     * @return bool
303
     */
304 256
    public function isDefaultTitleBlacklist(): bool
305
    {
306 256
        return $this->defaulttitleblacklist == 1;
307
    }
308
309
    /**
310
     * @param bool $isDefault
311
     */
312 256
    public function setDefaultTitleBlacklist(bool $isDefault): void
313
    {
314 256
        $this->defaulttitleblacklist = $isDefault ? 1 : 0;
315 256
    }
316
317
    /**
318
     * @return int
319
     */
320
    public function getDomain(): int
321
    {
322
        return $this->domain;
323
    }
324
325
    /**
326
     * @param int $domain
327
     */
328
    public function setDomain(int $domain): void
329
    {
330
        $this->domain = $domain;
331
    }
332
333
    /**
334
     * @return string
335
     */
336
    public function getApiName(): string
337
    {
338
        return $this->apiname;
339
    }
340
341
    /**
342
     * @param string $apiName
343
     */
344
    public function setApiName(string $apiName): void
345
    {
346
        $this->apiname = $apiName;
347
    }
348
349
    /**
350
     * @return string
351
     */
352
    public function getDisplayName(): string
353
    {
354
        return $this->displayname;
355
    }
356
357
    /**
358
     * @param string $displayName
359
     */
360
    public function setDisplayName(string $displayName): void
361
    {
362
        $this->displayname = $displayName;
363
    }
364
365
    /**
366
     * @return string
367
     */
368
    public function getHeader(): string
369
    {
370
        return $this->header;
371
    }
372
373
    /**
374
     * @param string $header
375
     */
376
    public function setHeader(string $header): void
377
    {
378
        $this->header = $header;
379
    }
380
381
    /**
382
     * @return string|null
383
     */
384
    public function getHelp(): ?string
385
    {
386
        return $this->help;
387
    }
388
389
    /**
390
     * @param string|null $help
391
     */
392
    public function setHelp(?string $help): void
393
    {
394
        $this->help = $help;
395
    }
396
397
    /**
398
     * @return string
399
     * @deprecated
400
     */
401
    public function getLogName(): string
402
    {
403
        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

403
        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...
404
    }
405
406
    /**
407
     * @param string $logName
408
     *
409
     * @deprecated
410
     */
411
    public function setLogName(string $logName): void
412
    {
413
        $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

413
        /** @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...
414
    }
415
416
    /**
417
     * @return string
418
     * @deprecated
419
     */
420
    public function getLegacyStatus(): string
421
    {
422
        return $this->legacystatus;
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$legacystatus has been deprecated: Removal due as part of #602 ( Ignorable by Annotation )

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

422
        return /** @scrutinizer ignore-deprecated */ $this->legacystatus;

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...
423
    }
424
425
    /**
426
     * @param string $legacyStatus
427
     *
428
     * @deprecated
429
     */
430
    public function setLegacyStatus(string $legacyStatus): void
431
    {
432
        $this->legacystatus = $legacyStatus;
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$legacystatus has been deprecated: Removal due as part of #602 ( Ignorable by Annotation )

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

432
        /** @scrutinizer ignore-deprecated */ $this->legacystatus = $legacyStatus;

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...
433
    }
434
435
436
437
}