Failed Conditions
Pull Request — user-welcomefix (#623)
by Simon
32:14 queued 18:22
created

Domain::setShortName()   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 Waca\DataObject;
13
use Waca\Exceptions\OptimisticLockFailedException;
14
15
class Domain extends DataObject
16
{
17
    /** @var string */
18
    private $shortname;
19
    /** @var string */
20
    private $longname;
21
    /** @var string */
22
    private $wikiarticlepath;
23
    /** @var string */
24
    private $wikiapipath;
25
    /** @var int */
26
    private $enabled = 0;
27
    /** @var int|null */
28
    private $defaultclose;
29
    /** @var string */
30
    private $defaultlanguage = 'en';
31
    /** @var string */
32
    private $emailsender;
33
    /** @var string|null */
34
    private $notificationtarget;
35
36
    public function save()
37
    {
38
        if ($this->isNew()) {
39
            // insert
40
            $statement = $this->dbObject->prepare(<<<SQL
41
                INSERT INTO domain (
42
                    shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, 
43
                    emailsender, notificationtarget
44
                ) VALUES (
45
                    :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage,
46
                    :emailsender, :notificationtarget
47
                );
48
SQL
49
            );
50
51
            $statement->bindValue(":shortname", $this->shortname);
52
            $statement->bindValue(":longname", $this->longname);
53
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
54
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
55
            $statement->bindValue(":enabled", $this->enabled);
56
            $statement->bindValue(":defaultclose", $this->defaultclose);
57
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
58
            $statement->bindValue(":emailsender", $this->emailsender);
59
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
60
61
            if ($statement->execute()) {
62
                $this->id = (int)$this->dbObject->lastInsertId();
63
            }
64
            else {
65
                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

65
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
66
            }
67
        }
68
        else {
69
            $statement = $this->dbObject->prepare(<<<SQL
70
                UPDATE domain SET
71
                    longname = :longname,
72
                    wikiarticlepath = :wikiarticlepath,
73
                    wikiapipath = :wikiapipath,
74
                    enabled = :enabled,
75
                    defaultclose = :defaultclose,
76
                    defaultlanguage = :defaultlanguage,
77
                    emailsender = :emailsender,
78
                    notificationtarget = :notificationtarget,
79
                
80
                    updateversion = updateversion + 1
81
				WHERE id = :id AND updateversion = :updateversion;
82
SQL
83
            );
84
85
            $statement->bindValue(":longname", $this->longname);
86
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
87
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
88
            $statement->bindValue(":enabled", $this->enabled);
89
            $statement->bindValue(":defaultclose", $this->defaultclose);
90
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
91
            $statement->bindValue(":emailsender", $this->emailsender);
92
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
93
94
            $statement->bindValue(':id', $this->id);
95
            $statement->bindValue(':updateversion', $this->updateversion);
96
97
            if (!$statement->execute()) {
98
                throw new Exception($statement->errorInfo());
99
            }
100
101
            if ($statement->rowCount() !== 1) {
102
                throw new OptimisticLockFailedException();
103
            }
104
105
            $this->updateversion++;
106
        }
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getShortName(): string
113
    {
114
        return $this->shortname;
115
    }
116
117
    /**
118
     * @param string $shortName
119
     */
120
    public function setShortName(string $shortName): void
121
    {
122
        $this->shortname = $shortName;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getLongName(): string
129
    {
130
        return $this->longname;
131
    }
132
133
    /**
134
     * @param string $longName
135
     */
136
    public function setLongName(string $longName): void
137
    {
138
        $this->longname = $longName;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getWikiArticlePath(): string
145
    {
146
        return $this->wikiarticlepath;
147
    }
148
149
    /**
150
     * @param string $wikiArticlePath
151
     */
152
    public function setWikiArticlePath(string $wikiArticlePath): void
153
    {
154
        $this->wikiarticlepath = $wikiArticlePath;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getWikiApiPath(): string
161
    {
162
        return $this->wikiapipath;
163
    }
164
165
    /**
166
     * @param string $wikiApiPath
167
     */
168
    public function setWikiApiPath(string $wikiApiPath): void
169
    {
170
        $this->wikiapipath = $wikiApiPath;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function isEnabled(): bool
177
    {
178
        return $this->enabled == 1;
179
    }
180
181
    /**
182
     * @param bool $enabled
183
     */
184
    public function setEnabled(bool $enabled): void
185
    {
186
        $this->enabled = $enabled ? 1 : 0;
187
    }
188
189
    /**
190
     * @return int
191
     */
192
    public function getDefaultClose(): ?int
193
    {
194
        return $this->defaultclose;
195
    }
196
197
    /**
198
     * @param int $defaultClose
199
     */
200
    public function setDefaultClose(?int $defaultClose): void
201
    {
202
        $this->defaultclose = $defaultClose;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getDefaultLanguage(): string
209
    {
210
        return $this->defaultlanguage;
211
    }
212
213
    /**
214
     * @param string $defaultLanguage
215
     */
216
    public function setDefaultLanguage(string $defaultLanguage): void
217
    {
218
        $this->defaultlanguage = $defaultLanguage;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function getEmailSender(): string
225
    {
226
        return $this->emailsender;
227
    }
228
229
    /**
230
     * @param string $emailSender
231
     */
232
    public function setEmailSender(string $emailSender): void
233
    {
234
        $this->emailsender = $emailSender;
235
    }
236
237
    /**
238
     * @return string|null
239
     */
240
    public function getNotificationTarget(): ?string
241
    {
242
        return $this->notificationtarget;
243
    }
244
245
    /**
246
     * @param string|null $notificationTarget
247
     */
248
    public function setNotificationTarget(?string $notificationTarget): void
249
    {
250
        $this->notificationtarget = $notificationTarget;
251
    }
252
253
254
}