Passed
Push — multiproject/domainui ( d92423 )
by Simon
04:27
created

Domain::getAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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 Domain extends DataObject
18
{
19
    /** @var string */
20
    private $shortname;
21
    /** @var string */
22
    private $longname;
23
    /** @var string */
24
    private $wikiarticlepath;
25
    /** @var string */
26
    private $wikiapipath;
27
    /** @var int */
28
    private $enabled = 0;
29
    /** @var int|null */
30
    private $defaultclose;
31
    /** @var string */
32
    private $defaultlanguage = 'en';
33
    /** @var string */
34
    private $emailsender;
35
    /** @var string|null */
36
    private $notificationtarget;
37
38
    /** @var Domain Cache variable of the current domain */
39
    private static $currentDomain;
40
41
    public static function getCurrent(PdoDatabase $database)
42
    {
43
        if (self::$currentDomain === null) {
44
            $sessionDomain = 1; // FIXME: #592 WebRequest::getSessionDomain();
45
46
            if ($sessionDomain !== null) {
0 ignored issues
show
introduced by
The condition $sessionDomain !== null is always true.
Loading history...
47
                /** @var Domain $domain */
48
                $domain = self::getById($sessionDomain, $database);
49
50
                if ($domain === false) {
0 ignored issues
show
introduced by
The condition $domain === false is always false.
Loading history...
51
                    self::$currentDomain = 1; // FIXME: #592 User::getCurrent($database)->getDefaultDomain();
52
                }
53
                else {
54
                    self::$currentDomain = $domain;
55
                }
56
            }
57
            else {
58
                self::$currentDomain = 1; // FIXME: #592 User::getCurrent($database)->getDefaultDomain();
59
            }
60
        }
61
62
        return self::$currentDomain;
63
    }
64
65
66
    public static function getAll(PdoDatabase $database) {
67
        $statement = $database->prepare("SELECT * FROM domain;");
68
        $statement->execute();
69
70
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
71
72
        /** @var Domain $t */
73
        foreach ($resultObject as $t) {
74
            $t->setDatabase($database);
75
        }
76
77
        return $resultObject;
78
    }
79
80
    public function save()
81
    {
82
        if ($this->isNew()) {
83
            // insert
84
            $statement = $this->dbObject->prepare(<<<SQL
85
                INSERT INTO domain (
86
                    shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, 
87
                    emailsender, notificationtarget
88
                ) VALUES (
89
                    :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage,
90
                    :emailsender, :notificationtarget
91
                );
92
SQL
93
            );
94
95
            $statement->bindValue(":shortname", $this->shortname);
96
            $statement->bindValue(":longname", $this->longname);
97
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
98
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
99
            $statement->bindValue(":enabled", $this->enabled);
100
            $statement->bindValue(":defaultclose", $this->defaultclose);
101
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
102
            $statement->bindValue(":emailsender", $this->emailsender);
103
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
104
105
            if ($statement->execute()) {
106
                $this->id = (int)$this->dbObject->lastInsertId();
107
            }
108
            else {
109
                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

109
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
110
            }
111
        }
112
        else {
113
            $statement = $this->dbObject->prepare(<<<SQL
114
                UPDATE domain SET
115
                    longname = :longname,
116
                    wikiarticlepath = :wikiarticlepath,
117
                    wikiapipath = :wikiapipath,
118
                    enabled = :enabled,
119
                    defaultclose = :defaultclose,
120
                    defaultlanguage = :defaultlanguage,
121
                    emailsender = :emailsender,
122
                    notificationtarget = :notificationtarget,
123
                
124
                    updateversion = updateversion + 1
125
				WHERE id = :id AND updateversion = :updateversion;
126
SQL
127
            );
128
129
            $statement->bindValue(":longname", $this->longname);
130
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
131
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
132
            $statement->bindValue(":enabled", $this->enabled);
133
            $statement->bindValue(":defaultclose", $this->defaultclose);
134
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
135
            $statement->bindValue(":emailsender", $this->emailsender);
136
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
137
138
            $statement->bindValue(':id', $this->id);
139
            $statement->bindValue(':updateversion', $this->updateversion);
140
141
            if (!$statement->execute()) {
142
                throw new Exception($statement->errorInfo());
143
            }
144
145
            if ($statement->rowCount() !== 1) {
146
                throw new OptimisticLockFailedException();
147
            }
148
149
            $this->updateversion++;
150
        }
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getShortName(): string
157
    {
158
        return $this->shortname;
159
    }
160
161
    /**
162
     * @param string $shortName
163
     */
164
    public function setShortName(string $shortName): void
165
    {
166
        $this->shortname = $shortName;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getLongName(): string
173
    {
174
        return $this->longname;
175
    }
176
177
    /**
178
     * @param string $longName
179
     */
180
    public function setLongName(string $longName): void
181
    {
182
        $this->longname = $longName;
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getWikiArticlePath(): string
189
    {
190
        return $this->wikiarticlepath;
191
    }
192
193
    /**
194
     * @param string $wikiArticlePath
195
     */
196
    public function setWikiArticlePath(string $wikiArticlePath): void
197
    {
198
        $this->wikiarticlepath = $wikiArticlePath;
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getWikiApiPath(): string
205
    {
206
        return $this->wikiapipath;
207
    }
208
209
    /**
210
     * @param string $wikiApiPath
211
     */
212
    public function setWikiApiPath(string $wikiApiPath): void
213
    {
214
        $this->wikiapipath = $wikiApiPath;
215
    }
216
217
    /**
218
     * @return bool
219
     */
220
    public function isEnabled(): bool
221
    {
222
        return $this->enabled == 1;
223
    }
224
225
    /**
226
     * @param bool $enabled
227
     */
228
    public function setEnabled(bool $enabled): void
229
    {
230
        $this->enabled = $enabled ? 1 : 0;
231
    }
232
233
    /**
234
     * @return int
235
     */
236
    public function getDefaultClose(): ?int
237
    {
238
        return $this->defaultclose;
239
    }
240
241
    /**
242
     * @param int $defaultClose
243
     */
244
    public function setDefaultClose(?int $defaultClose): void
245
    {
246
        $this->defaultclose = $defaultClose;
247
    }
248
249
    /**
250
     * @return string
251
     */
252
    public function getDefaultLanguage(): string
253
    {
254
        return $this->defaultlanguage;
255
    }
256
257
    /**
258
     * @param string $defaultLanguage
259
     */
260
    public function setDefaultLanguage(string $defaultLanguage): void
261
    {
262
        $this->defaultlanguage = $defaultLanguage;
263
    }
264
265
    /**
266
     * @return string
267
     */
268
    public function getEmailSender(): string
269
    {
270
        return $this->emailsender;
271
    }
272
273
    /**
274
     * @param string $emailSender
275
     */
276
    public function setEmailSender(string $emailSender): void
277
    {
278
        $this->emailsender = $emailSender;
279
    }
280
281
    /**
282
     * @return string|null
283
     */
284
    public function getNotificationTarget(): ?string
285
    {
286
        return $this->notificationtarget;
287
    }
288
289
    /**
290
     * @param string|null $notificationTarget
291
     */
292
    public function setNotificationTarget(?string $notificationTarget): void
293
    {
294
        $this->notificationtarget = $notificationTarget;
295
    }
296
297
298
}