Issues (186)

Branch: oauth-creation-featureflag

includes/DataObjects/RequestForm.php (1 issue)

Labels
Severity
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\DataObjects;
11
12
use Exception;
13
use PDO;
14
use Waca\DataObject;
15
use Waca\Exceptions\OptimisticLockFailedException;
16
use Waca\PdoDatabase;
17
18
class RequestForm extends DataObject
19
{
20
    /** @var int */
21
    private $enabled = 0;
22
    /** @var int */
23
    private $domain;
24
    /** @var string */
25
    private $name = '';
26
    /** @var string */
27
    private $publicendpoint = '';
28
    /** @var string */
29
    private $formcontent = '';
30
    /** @var int|null */
31
    private $overridequeue;
32
    /** @var string */
33
    private $usernamehelp;
34
    /** @var string */
35
    private $emailhelp;
36
    /** @var string */
37
    private $commentshelp;
38
39
    /**
40
     * @param PdoDatabase $database
41
     * @param int         $domain
42
     *
43
     * @return RequestForm[]
44
     */
45
    public static function getAllForms(PdoDatabase $database, int $domain)
46
    {
47
        $statement = $database->prepare("SELECT * FROM requestform WHERE domain = :domain;");
48
        $statement->execute([':domain' => $domain]);
49
50
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
51
52
        if ($resultObject === false) {
53
            return [];
54
        }
55
56
        /** @var RequestQueue $t */
57
        foreach ($resultObject as $t) {
58
            $t->setDatabase($database);
59
        }
60
61
        return $resultObject;
62
    }
63
64
    public static function getByName(PdoDatabase $database, string $name, int $domain)
65
    {
66
        $statement = $database->prepare(<<<SQL
67
            SELECT * FROM requestform WHERE name = :name AND domain = :domain;
68
SQL
69
        );
70
71
        $statement->execute([
72
            ':name' => $name,
73
            ':domain'  => $domain,
74
        ]);
75
76
        /** @var RequestForm|false $result */
77
        $result = $statement->fetchObject(get_called_class());
78
79
        if ($result !== false) {
80
            $result->setDatabase($database);
81
        }
82
83
        return $result;
84
    }
85
86
    public static function getByPublicEndpoint(PdoDatabase $database, string $endpoint, int $domain)
87
    {
88
        $statement = $database->prepare(<<<SQL
89
            SELECT * FROM requestform WHERE publicendpoint = :endpoint and domain = :domain;
90
SQL
91
        );
92
93
        $statement->execute([
94
            ':endpoint' => $endpoint,
95
            ':domain' => $domain,
96
        ]);
97
98
        /** @var RequestForm|false $result */
99
        $result = $statement->fetchObject(get_called_class());
100
101
        if ($result !== false) {
102
            $result->setDatabase($database);
103
        }
104
105
        return $result;
106
    }
107
108
    public function save()
109
    {
110
        if ($this->isNew()) {
111
            // insert
112
            $statement = $this->dbObject->prepare(<<<SQL
113
                INSERT INTO requestform (
114
                    enabled, domain, name, publicendpoint, formcontent, overridequeue, usernamehelp, emailhelp, commentshelp
115
                ) VALUES (
116
                    :enabled, :domain, :name, :publicendpoint, :formcontent, :overridequeue, :usernamehelp, :emailhelp, :commentshelp
117
                );
118
SQL
119
            );
120
121
            $statement->bindValue(":enabled", $this->enabled);
122
            $statement->bindValue(":domain", $this->domain);
123
            $statement->bindValue(":name", $this->name);
124
            $statement->bindValue(":publicendpoint", $this->publicendpoint);
125
            $statement->bindValue(":formcontent", $this->formcontent);
126
            $statement->bindValue(":overridequeue", $this->overridequeue);
127
            $statement->bindValue(":usernamehelp", $this->usernamehelp);
128
            $statement->bindValue(":emailhelp", $this->emailhelp);
129
            $statement->bindValue(":commentshelp", $this->commentshelp);
130
131
            if ($statement->execute()) {
132
                $this->id = (int)$this->dbObject->lastInsertId();
133
            }
134
            else {
135
                throw new Exception($statement->errorInfo());
0 ignored issues
show
$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

135
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
136
            }
137
        }
138
        else {
139
            $statement = $this->dbObject->prepare(<<<SQL
140
                UPDATE requestform SET
141
                    enabled = :enabled,
142
                    domain = :domain,
143
                    name = :name,
144
                    publicendpoint = :publicendpoint,
145
                    formcontent = :formcontent,
146
                    overridequeue = :overridequeue,
147
                    usernamehelp = :usernamehelp,
148
                    emailhelp = :emailhelp,
149
                    commentshelp = :commentshelp,
150
                
151
                    updateversion = updateversion + 1
152
				WHERE id = :id AND updateversion = :updateversion;
153
SQL
154
            );
155
156
            $statement->bindValue(":enabled", $this->enabled);
157
            $statement->bindValue(":domain", $this->domain);
158
            $statement->bindValue(":name", $this->name);
159
            $statement->bindValue(":publicendpoint", $this->publicendpoint);
160
            $statement->bindValue(":formcontent", $this->formcontent);
161
            $statement->bindValue(":overridequeue", $this->overridequeue);
162
            $statement->bindValue(":usernamehelp", $this->usernamehelp);
163
            $statement->bindValue(":emailhelp", $this->emailhelp);
164
            $statement->bindValue(":commentshelp", $this->commentshelp);
165
166
167
            $statement->bindValue(':id', $this->id);
168
            $statement->bindValue(':updateversion', $this->updateversion);
169
170
            if (!$statement->execute()) {
171
                throw new Exception($statement->errorInfo());
172
            }
173
174
            if ($statement->rowCount() !== 1) {
175
                throw new OptimisticLockFailedException();
176
            }
177
178
            $this->updateversion++;
179
        }
180
    }
181
182
    /**
183
     * @return bool
184
     */
185
    public function isEnabled(): bool
186
    {
187
        return $this->enabled == 1;
188
    }
189
190
    /**
191
     * @param bool $enabled
192
     */
193
    public function setEnabled(bool $enabled): void
194
    {
195
        $this->enabled = $enabled ? 1 : 0;
196
    }
197
198
    /**
199
     * @return int
200
     */
201
    public function getDomain(): int
202
    {
203
        return $this->domain;
204
    }
205
206
    public function getDomainObject(): ?Domain
207
    {
208
        if ($this->domain !== null) {
209
            /** @var Domain $domain */
210
            $domain = Domain::getById($this->domain, $this->getDatabase());
211
            return $domain;
212
        }
213
214
        return null;
215
    }
216
217
    /**
218
     * @param int $domain
219
     */
220
    public function setDomain(int $domain): void
221
    {
222
        $this->domain = $domain;
223
    }
224
225
    /**
226
     * @return string
227
     */
228
    public function getName(): string
229
    {
230
        return $this->name;
231
    }
232
233
    /**
234
     * @param string $name
235
     */
236
    public function setName(string $name): void
237
    {
238
        $this->name = $name;
239
    }
240
241
    /**
242
     * @return string
243
     */
244
    public function getPublicEndpoint(): string
245
    {
246
        return $this->publicendpoint;
247
    }
248
249
    /**
250
     * @param string $publicEndpoint
251
     */
252
    public function setPublicEndpoint(string $publicEndpoint): void
253
    {
254
        $this->publicendpoint = $publicEndpoint;
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    public function getFormContent(): string
261
    {
262
        return $this->formcontent;
263
    }
264
265
    /**
266
     * @param string $formContent
267
     */
268
    public function setFormContent(string $formContent): void
269
    {
270
        $this->formcontent = $formContent;
271
    }
272
273
    /**
274
     * @return int|null
275
     */
276
    public function getOverrideQueue(): ?int
277
    {
278
        return $this->overridequeue;
279
    }
280
281
    /**
282
     * @param int|null $overrideQueue
283
     */
284
    public function setOverrideQueue(?int $overrideQueue): void
285
    {
286
        $this->overridequeue = $overrideQueue;
287
    }
288
289
    /**
290
     * @return string
291
     */
292
    public function getUsernameHelp(): ?string
293
    {
294
        return $this->usernamehelp;
295
    }
296
297
    /**
298
     * @param string $usernamehelp
299
     */
300
    public function setUsernameHelp(string $usernamehelp): void
301
    {
302
        $this->usernamehelp = $usernamehelp;
303
    }
304
305
    /**
306
     * @return string
307
     */
308
    public function getEmailHelp(): ?string
309
    {
310
        return $this->emailhelp;
311
    }
312
313
    /**
314
     * @param string $emailhelp
315
     */
316
    public function setEmailHelp(string $emailhelp): void
317
    {
318
        $this->emailhelp = $emailhelp;
319
    }
320
321
    /**
322
     * @return string
323
     */
324
    public function getCommentHelp(): ?string
325
    {
326
        return $this->commentshelp;
327
    }
328
329
    /**
330
     * @param string $commenthelp
331
     */
332
    public function setCommentHelp(string $commenthelp): void
333
    {
334
        $this->commentshelp = $commenthelp;
335
    }
336
}