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

EmailTemplate::setText()   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 0
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
17
/**
18
 * Email template data object
19
 *
20
 * This is the close reasons thing.
21
 */
22
class EmailTemplate extends DataObject
23
{
24
    const ACTION_CREATED = 'created';
25
    const ACTION_NOT_CREATED = 'not created';
26
    const ACTION_NONE = 'none';
27
    const ACTION_DEFER = 'defer';
28
29
    /** @var string the name of the template */
30
    private $name;
31
    private $text;
32
    /** @var string|null */
33
    private $jsquestion;
34
    private $active = 1;
35
    private $preloadonly = 0;
36
    private $defaultaction = self::ACTION_NOT_CREATED;
37
    private $queue;
38
39
    /**
40
     * Gets active non-preload templates
41
     *
42
     * @param string      $defaultAction Default action to take (EmailTemplate::ACTION_CREATED or EmailTemplate::ACTION_NOT_CREATED)
43
     * @param PdoDatabase $database
44
     * @param int|null    $filter Template IDs to filter out
45
     *
46
     * @return array|false
47
     */
48
    public static function getActiveNonpreloadTemplates($defaultAction, PdoDatabase $database, ?int $filter = null)
49
    {
50
        $statement = $database->prepare(<<<SQL
51
SELECT * FROM `emailtemplate`
52
WHERE defaultaction = :forcreated AND active = 1 AND preloadonly = 0 AND (:skipFilter = 1 OR id <> :filter);
53
SQL
54
        );
55
        $statement->bindValue(":forcreated", $defaultAction);
56
        $statement->bindValue(":filter", $filter);
57
        $statement->bindValue(":skipFilter", $filter === null ? 1 : 0);
58
59
        $statement->execute();
60
61
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
62
63
        /** @var EmailTemplate $t */
64
        foreach ($resultObject as $t) {
65
            $t->setDatabase($database);
66
        }
67
68
        return $resultObject;
69
    }
70
71
    /**
72
     * Gets active non-preload and preload templates, optionally filtered by the default action.
73
     *
74
     * @param null|bool|string $defaultAction Default action to take (EmailTemplate::ACTION_CREATED,
75
     *                                        EmailTemplate::ACTION_NOT_CREATED, or EmailTemplate::ACTION_NONE), or optionally null to
76
     *                                        just get everything.
77
     * @param PdoDatabase      $database
78
     *
79
     * @return array|false
80
     */
81
    public static function getAllActiveTemplates($defaultAction, PdoDatabase $database)
82
    {
83
        $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE defaultaction = :forcreated AND active = 1;");
84
85
        if ($defaultAction === false) {
86
            $statement = $database->prepare(
87
                "SELECT * FROM `emailtemplate` WHERE defaultaction NOT IN ('created', 'not created') AND active = 1;");
88
        }
89
90
        if ($defaultAction === null) {
91
            $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE  active = 1;");
92
        }
93
94
        $statement->bindValue(":forcreated", $defaultAction);
95
96
        $statement->execute();
97
98
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
99
100
        /** @var EmailTemplate $t */
101
        foreach ($resultObject as $t) {
102
            $t->setDatabase($database);
103
        }
104
105
        return $resultObject;
106
    }
107
108
    /**
109
     * Gets all the inactive templates
110
     *
111
     * @param PdoDatabase $database
112
     *
113
     * @return array
114
     */
115
    public static function getAllInactiveTemplates(PdoDatabase $database)
116
    {
117
        $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE  active = 0;");
118
        $statement->execute();
119
120
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
121
122
        /** @var EmailTemplate $t */
123
        foreach ($resultObject as $t) {
124
            $t->setDatabase($database);
125
        }
126
127
        return $resultObject;
128
    }
129
130
    /**
131
     * @param string      $name
132
     * @param PdoDatabase $database
133
     *
134
     * @return EmailTemplate|false
135
     */
136
    public static function getByName($name, PdoDatabase $database)
137
    {
138
        $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE name = :name LIMIT 1;");
139
        $statement->bindValue(":name", $name);
140
141
        $statement->execute();
142
143
        $resultObject = $statement->fetchObject(get_called_class());
144
145
        if ($resultObject != false) {
146
            $resultObject->setDatabase($database);
147
        }
148
149
        return $resultObject;
150
    }
151
152
    /**
153
     * @return EmailTemplate
154
     */
155
    public static function getDroppedTemplate()
156
    {
157
        $t = new EmailTemplate();
158
        $t->id = 0;
159
        $t->active = 1;
160
        $t->defaultaction = self::ACTION_NONE;
161
        $t->name = 'Dropped';
162
163
        return $t;
164
    }
165
166
    /**
167
     * @throws Exception
168
     */
169
    public function save()
170
    {
171
        if ($this->isNew()) {
172
            // insert
173
            $statement = $this->dbObject->prepare(<<<SQL
174
INSERT INTO `emailtemplate` (name, text, jsquestion, defaultaction, active, preloadonly, queue)
175
VALUES (:name, :text, :jsquestion, :defaultaction, :active, :preloadonly, :queue);
176
SQL
177
            );
178
            $statement->bindValue(":name", $this->name);
179
            $statement->bindValue(":text", $this->text);
180
            $statement->bindValue(":jsquestion", $this->jsquestion);
181
            $statement->bindValue(":defaultaction", $this->defaultaction);
182
            $statement->bindValue(":active", $this->active);
183
            $statement->bindValue(":preloadonly", $this->preloadonly);
184
            $statement->bindValue(":queue", $this->queue);
185
186
            if ($statement->execute()) {
187
                $this->id = (int)$this->dbObject->lastInsertId();
188
            }
189
            else {
190
                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

190
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
191
            }
192
        }
193
        else {
194
            // update
195
            $statement = $this->dbObject->prepare(<<<SQL
196
UPDATE `emailtemplate`
197
SET name = :name,
198
	text = :text,
199
	jsquestion = :jsquestion,
200
	defaultaction = :defaultaction,
201
	active = :active,
202
	preloadonly = :preloadonly,
203
    queue = :queue,
204
	updateversion = updateversion + 1
205
WHERE id = :id AND updateversion = :updateversion;
206
SQL
207
            );
208
            $statement->bindValue(':id', $this->id);
209
            $statement->bindValue(':updateversion', $this->updateversion);
210
211
            $statement->bindValue(':name', $this->name);
212
            $statement->bindValue(":text", $this->text);
213
            $statement->bindValue(":jsquestion", $this->jsquestion);
214
            $statement->bindValue(":defaultaction", $this->defaultaction);
215
            $statement->bindValue(":active", $this->active);
216
            $statement->bindValue(":preloadonly", $this->preloadonly);
217
            $statement->bindValue(":queue", $this->queue);
218
219
            if (!$statement->execute()) {
220
                throw new Exception($statement->errorInfo());
221
            }
222
223
            if ($statement->rowCount() !== 1) {
224
                throw new OptimisticLockFailedException();
225
            }
226
227
            $this->updateversion++;
228
        }
229
    }
230
231
    /**
232
     * Override delete() from DataObject
233
     */
234
    public function delete()
235
    {
236
        throw new Exception("You shouldn't be doing that, you'll break logs.");
237
    }
238
239
    /**
240
     * @return string
241
     */
242
    public function getName()
243
    {
244
        return $this->name;
245
    }
246
247
    /**
248
     * @param string $name
249
     */
250
    public function setName($name)
251
    {
252
        $this->name = $name;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function getText()
259
    {
260
        return $this->text;
261
    }
262
263
    /**
264
     * @param string $text
265
     */
266
    public function setText($text)
267
    {
268
        $this->text = $text;
269
    }
270
271
    /**
272
     * @return string|null
273
     */
274
    public function getJsquestion()
275
    {
276
        return $this->jsquestion;
277
    }
278
279
    /**
280
     * @param string $jsquestion
281
     */
282
    public function setJsquestion($jsquestion)
283
    {
284
        $this->jsquestion = $jsquestion;
285
    }
286
287
    /**
288
     * @return string
289
     */
290
    public function getDefaultAction()
291
    {
292
        return $this->defaultaction;
293
    }
294
295
    /**
296
     * @param string $defaultAction
297
     */
298
    public function setDefaultAction($defaultAction)
299
    {
300
        $this->defaultaction = $defaultAction;
301
    }
302
303
    /**
304
     * @return bool
305
     */
306
    public function getActive()
307
    {
308
        return $this->active == 1;
309
    }
310
311
    /**
312
     * @param bool $active
313
     */
314
    public function setActive($active)
315
    {
316
        $this->active = $active ? 1 : 0;
317
    }
318
319
    /**
320
     * @return bool
321
     */
322
    public function getPreloadOnly()
323
    {
324
        return $this->preloadonly == 1;
325
    }
326
327
    /**
328
     * @param bool $preloadonly
329
     */
330
    public function setPreloadOnly($preloadonly)
331
    {
332
        $this->preloadonly = $preloadonly ? 1 : 0;
333
    }
334
335
    /**
336
     * @return int|null
337
     */
338
    public function getQueue(): ?int
339
    {
340
        return $this->queue;
341
    }
342
343
    /**
344
     * @return RequestQueue|null
345
     */
346
    public function getQueueObject(): ?RequestQueue
347
    {
348
        if ($this->queue === null) {
349
            return null;
350
        }
351
352
        /** @var $dataObject RequestQueue|false */
353
        $dataObject = RequestQueue::getById($this->queue, $this->getDatabase());
354
355
        if ($dataObject === false) {
0 ignored issues
show
introduced by
The condition $dataObject === false is always false.
Loading history...
356
            return null;
357
        }
358
359
        return $dataObject;
360
    }
361
362
    /**
363
     * @param int|null $queue
364
     */
365
    public function setQueue(?int $queue): void
366
    {
367
        $this->queue = $queue;
368
    }
369
}
370