Passed
Push — multiproject/local-access ( 5353e5...767924 )
by Simon
03:31
created

Domain::getDomainByUser()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 24
rs 9.7666
c 0
b 0
f 0
cc 4
nc 2
nop 3
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 $emailreplyaddress;
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 function save()
104
    {
105
        if ($this->isNew()) {
106
            // insert
107
            $statement = $this->dbObject->prepare(<<<SQL
108
                INSERT INTO domain (
109
                    shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, 
110
                    emailreplyaddress, notificationtarget, localdocumentation
111
                ) VALUES (
112
                    :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage,
113
                    :emailreplyaddress, :notificationtarget, :localdocumentation
114
                );
115
SQL
116
            );
117
118
            $statement->bindValue(":shortname", $this->shortname);
119
            $statement->bindValue(":longname", $this->longname);
120
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
121
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
122
            $statement->bindValue(":enabled", $this->enabled);
123
            $statement->bindValue(":defaultclose", $this->defaultclose);
124
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
125
            $statement->bindValue(":emailreplyaddress", $this->emailreplyaddress);
126
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
127
            $statement->bindValue(":localdocumentation", $this->localdocumentation);
128
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 domain SET
140
                    longname = :longname,
141
                    wikiarticlepath = :wikiarticlepath,
142
                    wikiapipath = :wikiapipath,
143
                    enabled = :enabled,
144
                    defaultclose = :defaultclose,
145
                    defaultlanguage = :defaultlanguage,
146
                    emailreplyaddress = :emailreplyaddress,
147
                    notificationtarget = :notificationtarget,
148
                    localdocumentation = :localdocumentation,
149
                
150
                    updateversion = updateversion + 1
151
				WHERE id = :id AND updateversion = :updateversion;
152
SQL
153
            );
154
155
            $statement->bindValue(":longname", $this->longname);
156
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
157
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
158
            $statement->bindValue(":enabled", $this->enabled);
159
            $statement->bindValue(":defaultclose", $this->defaultclose);
160
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
161
            $statement->bindValue(":emailreplyaddress", $this->emailreplyaddress);
162
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
163
            $statement->bindValue(":localdocumentation", $this->localdocumentation);
164
165
            $statement->bindValue(':id', $this->id);
166
            $statement->bindValue(':updateversion', $this->updateversion);
167
168
            if (!$statement->execute()) {
169
                throw new Exception($statement->errorInfo());
170
            }
171
172
            if ($statement->rowCount() !== 1) {
173
                throw new OptimisticLockFailedException();
174
            }
175
176
            $this->updateversion++;
177
        }
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function getShortName(): string
184
    {
185
        return $this->shortname;
186
    }
187
188
    /**
189
     * @param string $shortName
190
     */
191
    public function setShortName(string $shortName): void
192
    {
193
        $this->shortname = $shortName;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getLongName(): string
200
    {
201
        return $this->longname;
202
    }
203
204
    /**
205
     * @param string $longName
206
     */
207
    public function setLongName(string $longName): void
208
    {
209
        $this->longname = $longName;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function getWikiArticlePath(): string
216
    {
217
        return $this->wikiarticlepath;
218
    }
219
220
    /**
221
     * @param string $wikiArticlePath
222
     */
223
    public function setWikiArticlePath(string $wikiArticlePath): void
224
    {
225
        $this->wikiarticlepath = $wikiArticlePath;
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getWikiApiPath(): string
232
    {
233
        return $this->wikiapipath;
234
    }
235
236
    /**
237
     * @param string $wikiApiPath
238
     */
239
    public function setWikiApiPath(string $wikiApiPath): void
240
    {
241
        $this->wikiapipath = $wikiApiPath;
242
    }
243
244
    /**
245
     * @return bool
246
     */
247
    public function isEnabled(): bool
248
    {
249
        return $this->enabled == 1;
250
    }
251
252
    /**
253
     * @param bool $enabled
254
     */
255
    public function setEnabled(bool $enabled): void
256
    {
257
        $this->enabled = $enabled ? 1 : 0;
258
    }
259
260
    /**
261
     * @return int
262
     */
263
    public function getDefaultClose(): ?int
264
    {
265
        return $this->defaultclose;
266
    }
267
268
    /**
269
     * @param int $defaultClose
270
     */
271
    public function setDefaultClose(?int $defaultClose): void
272
    {
273
        $this->defaultclose = $defaultClose;
274
    }
275
276
    /**
277
     * @return string
278
     */
279
    public function getDefaultLanguage(): string
280
    {
281
        return $this->defaultlanguage;
282
    }
283
284
    /**
285
     * @param string $defaultLanguage
286
     */
287
    public function setDefaultLanguage(string $defaultLanguage): void
288
    {
289
        $this->defaultlanguage = $defaultLanguage;
290
    }
291
292
    /**
293
     * @return string
294
     */
295
    public function getEmailReplyAddress(): string
296
    {
297
        return $this->emailreplyaddress;
298
    }
299
300
    /**
301
     * @param string $emailReplyAddress
302
     */
303
    public function setEmailReplyAddress(string $emailReplyAddress): void
304
    {
305
        $this->emailreplyaddress = $emailReplyAddress;
306
    }
307
308
    /**
309
     * @return string|null
310
     */
311
    public function getNotificationTarget(): ?string
312
    {
313
        return $this->notificationtarget;
314
    }
315
316
    /**
317
     * @param string|null $notificationTarget
318
     */
319
    public function setNotificationTarget(?string $notificationTarget): void
320
    {
321
        $this->notificationtarget = $notificationTarget;
322
    }
323
324
    /**
325
     * @return string
326
     */
327
    public function getLocalDocumentation(): string
328
    {
329
        return $this->localdocumentation;
330
    }
331
332
    /**
333
     * @param string $localDocumentation
334
     */
335
    public function setLocalDocumentation(string $localDocumentation): void
336
    {
337
        $this->localdocumentation = $localDocumentation;
338
    }
339
}