Passed
Push — search ( bdb480...5e7f6e )
by Simon
17:14 queued 07:17
created

Domain::setDefaultClose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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
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
39
    /** @var Domain Cache variable of the current domain */
40
    private static $currentDomain;
41
42
    public static function getCurrent(PdoDatabase $database)
43
    {
44
        if (self::$currentDomain === null) {
45
            $sessionDomain = WebRequest::getSessionDomain();
46
47
            if ($sessionDomain !== null) {
48
                /** @var Domain $domain */
49
                $domain = self::getById($sessionDomain, $database);
50
51
                if ($domain === false) {
0 ignored issues
show
introduced by
The condition $domain === false is always false.
Loading history...
52
                    self::$currentDomain = self::getById(1, $database); // FIXME: #594 User::getCurrent($database)->getDefaultDomain();
53
                }
54
                else {
55
                    self::$currentDomain = $domain;
56
                }
57
            }
58
            else {
59
                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...
60
            }
61
        }
62
63
        return self::$currentDomain;
64
    }
65
66
67
    public static function getAll(PdoDatabase $database) {
68
        $statement = $database->prepare("SELECT * FROM domain;");
69
        $statement->execute();
70
71
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
72
73
        /** @var Domain $t */
74
        foreach ($resultObject as $t) {
75
            $t->setDatabase($database);
76
        }
77
78
        return $resultObject;
79
    }
80
81
    public static function getDomainByUser(PdoDatabase $database, User $user, ?bool $enabled = null)
82
    {
83
        $statement = $database->prepare(<<<'SQL'
84
            SELECT d.* 
85
            FROM domain d
86
            INNER JOIN userdomain ud on d.id = ud.domain
87
            WHERE ud.user = :user
88
            AND (:filterEnabled = 0 OR d.enabled = :enabled)
89
SQL
90
);
91
        $statement->execute([
92
            ':user' => $user->getId(),
93
            ':filterEnabled' => ($enabled === null) ? 0 : 1,
94
            ':enabled' => ($enabled) ? 1 : 0
95
        ]);
96
97
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
98
99
        /** @var Domain $t */
100
        foreach ($resultObject as $t) {
101
            $t->setDatabase($database);
102
        }
103
104
        return $resultObject;
105
    }
106
107
    public function save()
108
    {
109
        if ($this->isNew()) {
110
            // insert
111
            $statement = $this->dbObject->prepare(<<<SQL
112
                INSERT INTO domain (
113
                    shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, 
114
                    emailsender, notificationtarget
115
                ) VALUES (
116
                    :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage,
117
                    :emailsender, :notificationtarget
118
                );
119
SQL
120
            );
121
122
            $statement->bindValue(":shortname", $this->shortname);
123
            $statement->bindValue(":longname", $this->longname);
124
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
125
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
126
            $statement->bindValue(":enabled", $this->enabled);
127
            $statement->bindValue(":defaultclose", $this->defaultclose);
128
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
129
            $statement->bindValue(":emailsender", $this->emailsender);
130
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
131
132
            if ($statement->execute()) {
133
                $this->id = (int)$this->dbObject->lastInsertId();
134
            }
135
            else {
136
                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

136
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
137
            }
138
        }
139
        else {
140
            $statement = $this->dbObject->prepare(<<<SQL
141
                UPDATE domain SET
142
                    longname = :longname,
143
                    wikiarticlepath = :wikiarticlepath,
144
                    wikiapipath = :wikiapipath,
145
                    enabled = :enabled,
146
                    defaultclose = :defaultclose,
147
                    defaultlanguage = :defaultlanguage,
148
                    emailsender = :emailsender,
149
                    notificationtarget = :notificationtarget,
150
                
151
                    updateversion = updateversion + 1
152
				WHERE id = :id AND updateversion = :updateversion;
153
SQL
154
            );
155
156
            $statement->bindValue(":longname", $this->longname);
157
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
158
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
159
            $statement->bindValue(":enabled", $this->enabled);
160
            $statement->bindValue(":defaultclose", $this->defaultclose);
161
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
162
            $statement->bindValue(":emailsender", $this->emailsender);
163
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
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 getEmailSender(): string
296
    {
297
        return $this->emailsender;
298
    }
299
300
    /**
301
     * @param string $emailSender
302
     */
303
    public function setEmailSender(string $emailSender): void
304
    {
305
        $this->emailsender = $emailSender;
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
}