Passed
Pull Request — bugfix (#733)
by Simon
08:02 queued 05:20
created

Domain::setLocalDocumentation()   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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
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
use Waca\WebRequest;
17
18
class Domain extends DataObject
19
{
20
    /** @var string */
21
    private $shortname;
22
    /** @var string */
23
    private $longname;
24
    /** @var string */
25
    private $wikiarticlepath;
26
    /** @var string */
27
    private $wikiapipath;
28
    /** @var int */
29
    private $enabled = 0;
30
    /** @var int|null */
31
    private $defaultclose;
32
    /** @var string */
33
    private $defaultlanguage = 'en';
34
    /** @var string */
35
    private $emailsender;
36
    /** @var string|null */
37
    private $notificationtarget;
38
    /** @var string */
39
    private $localdocumentation;
40
41
    /** @var Domain Cache variable of the current domain */
42
    private static $currentDomain;
43
44
    public static function getCurrent(PdoDatabase $database)
45
    {
46
        if (self::$currentDomain === null) {
47
            $sessionDomain = WebRequest::getSessionDomain();
48
49
            if ($sessionDomain !== null) {
50
                /** @var Domain $domain */
51
                $domain = self::getById($sessionDomain, $database);
52
53
                if ($domain === false) {
0 ignored issues
show
introduced by
The condition $domain === false is always false.
Loading history...
54
                    self::$currentDomain = self::getById(1, $database); // FIXME: #594 User::getCurrent($database)->getDefaultDomain();
55
                }
56
                else {
57
                    self::$currentDomain = $domain;
58
                }
59
            }
60
            else {
61
                self::$currentDomain = self::getById(1, $database); // FIXME: #594 User::getCurrent($database)->getDefaultDomain();
0 ignored issues
show
Documentation Bug introduced by
It seems like self::getById(1, $database) of type false is incompatible with the declared type Waca\DataObjects\Domain of property $currentDomain.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
            }
63
        }
64
65
        return self::$currentDomain;
66
    }
67
68
    public static function getByShortName(string $shortName, PdoDatabase $database)
69
    {
70
        $statement = $database->prepare(<<<SQL
71
            SELECT * FROM domain WHERE shortname = :name;
72
SQL
73
        );
74
75
        $statement->execute([
76
            ':name' => $shortName,
77
        ]);
78
79
        /** @var RequestForm|false $result */
80
        $result = $statement->fetchObject(get_called_class());
81
82
        if ($result !== false) {
83
            $result->setDatabase($database);
84
        }
85
86
        return $result;
87
    }
88
89
    public static function getAll(PdoDatabase $database) {
90
        $statement = $database->prepare("SELECT * FROM domain;");
91
        $statement->execute();
92
93
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
94
95
        /** @var Domain $t */
96
        foreach ($resultObject as $t) {
97
            $t->setDatabase($database);
98
        }
99
100
        return $resultObject;
101
    }
102
103
    public static function getDomainByUser(PdoDatabase $database, User $user, ?bool $enabled = null)
104
    {
105
        $statement = $database->prepare(<<<'SQL'
106
            SELECT d.* 
107
            FROM domain d
108
            INNER JOIN userdomain ud on d.id = ud.domain
109
            WHERE ud.user = :user
110
            AND (:filterEnabled = 0 OR d.enabled = :enabled)
111
SQL
112
);
113
        $statement->execute([
114
            ':user' => $user->getId(),
115
            ':filterEnabled' => ($enabled === null) ? 0 : 1,
116
            ':enabled' => ($enabled) ? 1 : 0
117
        ]);
118
119
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
120
121
        /** @var Domain $t */
122
        foreach ($resultObject as $t) {
123
            $t->setDatabase($database);
124
        }
125
126
        return $resultObject;
127
    }
128
129
    public function save()
130
    {
131
        if ($this->isNew()) {
132
            // insert
133
            $statement = $this->dbObject->prepare(<<<SQL
134
                INSERT INTO domain (
135
                    shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, 
136
                    emailsender, notificationtarget, localdocumentation
137
                ) VALUES (
138
                    :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage,
139
                    :emailsender, :notificationtarget, :localdocumentation
140
                );
141
SQL
142
            );
143
144
            $statement->bindValue(":shortname", $this->shortname);
145
            $statement->bindValue(":longname", $this->longname);
146
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
147
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
148
            $statement->bindValue(":enabled", $this->enabled);
149
            $statement->bindValue(":defaultclose", $this->defaultclose);
150
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
151
            $statement->bindValue(":emailsender", $this->emailsender);
152
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
153
            $statement->bindValue(":localdocumentation", $this->localdocumentation);
154
155
156
            if ($statement->execute()) {
157
                $this->id = (int)$this->dbObject->lastInsertId();
158
            }
159
            else {
160
                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

160
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
161
            }
162
        }
163
        else {
164
            $statement = $this->dbObject->prepare(<<<SQL
165
                UPDATE domain SET
166
                    longname = :longname,
167
                    wikiarticlepath = :wikiarticlepath,
168
                    wikiapipath = :wikiapipath,
169
                    enabled = :enabled,
170
                    defaultclose = :defaultclose,
171
                    defaultlanguage = :defaultlanguage,
172
                    emailsender = :emailsender,
173
                    notificationtarget = :notificationtarget,
174
                    localdocumentation = :localdocumentation,
175
                
176
                    updateversion = updateversion + 1
177
				WHERE id = :id AND updateversion = :updateversion;
178
SQL
179
            );
180
181
            $statement->bindValue(":longname", $this->longname);
182
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
183
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
184
            $statement->bindValue(":enabled", $this->enabled);
185
            $statement->bindValue(":defaultclose", $this->defaultclose);
186
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
187
            $statement->bindValue(":emailsender", $this->emailsender);
188
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
189
            $statement->bindValue(":localdocumentation", $this->localdocumentation);
190
191
            $statement->bindValue(':id', $this->id);
192
            $statement->bindValue(':updateversion', $this->updateversion);
193
194
            if (!$statement->execute()) {
195
                throw new Exception($statement->errorInfo());
196
            }
197
198
            if ($statement->rowCount() !== 1) {
199
                throw new OptimisticLockFailedException();
200
            }
201
202
            $this->updateversion++;
203
        }
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getShortName(): string
210
    {
211
        return $this->shortname;
212
    }
213
214
    /**
215
     * @param string $shortName
216
     */
217
    public function setShortName(string $shortName): void
218
    {
219
        $this->shortname = $shortName;
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function getLongName(): string
226
    {
227
        return $this->longname;
228
    }
229
230
    /**
231
     * @param string $longName
232
     */
233
    public function setLongName(string $longName): void
234
    {
235
        $this->longname = $longName;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getWikiArticlePath(): string
242
    {
243
        return $this->wikiarticlepath;
244
    }
245
246
    /**
247
     * @param string $wikiArticlePath
248
     */
249
    public function setWikiArticlePath(string $wikiArticlePath): void
250
    {
251
        $this->wikiarticlepath = $wikiArticlePath;
252
    }
253
254
    /**
255
     * @return string
256
     */
257
    public function getWikiApiPath(): string
258
    {
259
        return $this->wikiapipath;
260
    }
261
262
    /**
263
     * @param string $wikiApiPath
264
     */
265
    public function setWikiApiPath(string $wikiApiPath): void
266
    {
267
        $this->wikiapipath = $wikiApiPath;
268
    }
269
270
    /**
271
     * @return bool
272
     */
273
    public function isEnabled(): bool
274
    {
275
        return $this->enabled == 1;
276
    }
277
278
    /**
279
     * @param bool $enabled
280
     */
281
    public function setEnabled(bool $enabled): void
282
    {
283
        $this->enabled = $enabled ? 1 : 0;
284
    }
285
286
    /**
287
     * @return int
288
     */
289
    public function getDefaultClose(): ?int
290
    {
291
        return $this->defaultclose;
292
    }
293
294
    /**
295
     * @param int $defaultClose
296
     */
297
    public function setDefaultClose(?int $defaultClose): void
298
    {
299
        $this->defaultclose = $defaultClose;
300
    }
301
302
    /**
303
     * @return string
304
     */
305
    public function getDefaultLanguage(): string
306
    {
307
        return $this->defaultlanguage;
308
    }
309
310
    /**
311
     * @param string $defaultLanguage
312
     */
313
    public function setDefaultLanguage(string $defaultLanguage): void
314
    {
315
        $this->defaultlanguage = $defaultLanguage;
316
    }
317
318
    /**
319
     * @return string
320
     */
321
    public function getEmailSender(): string
322
    {
323
        return $this->emailsender;
324
    }
325
326
    /**
327
     * @param string $emailSender
328
     */
329
    public function setEmailSender(string $emailSender): void
330
    {
331
        $this->emailsender = $emailSender;
332
    }
333
334
    /**
335
     * @return string|null
336
     */
337
    public function getNotificationTarget(): ?string
338
    {
339
        return $this->notificationtarget;
340
    }
341
342
    /**
343
     * @param string|null $notificationTarget
344
     */
345
    public function setNotificationTarget(?string $notificationTarget): void
346
    {
347
        $this->notificationtarget = $notificationTarget;
348
    }
349
350
    /**
351
     * @return string
352
     */
353
    public function getLocalDocumentation(): string
354
    {
355
        return $this->localdocumentation;
356
    }
357
358
    /**
359
     * @param string $localDocumentation
360
     */
361
    public function setLocalDocumentation(string $localDocumentation): void
362
    {
363
        $this->localdocumentation = $localDocumentation;
364
    }
365
}