Passed
Push — multiproject/requestforms ( 675fe5 )
by Simon
08:58 queued 04:51
created

RequestForm   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 33
eloc 113
c 1
b 0
f 0
dl 0
loc 317
ccs 0
cts 187
cp 0
rs 9.76

23 Methods

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

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