SiteNotice::save()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 0
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
use Waca\PdoDatabase;
15
16
/**
17
 * Interface data object
18
 *
19
 * Interface messages for those messages which are not templates.
20
 */
21
class SiteNotice extends DataObject
22
{
23
    /** @var string */
24
    private $content;
25
26
    /**
27
     * Get a message.
28
     *
29
     * @param PdoDatabase $database
30
     *
31
     * @return string The content for display
32
     */
33
    public static function get(PdoDatabase $database)
34
    {
35
        /** @var SiteNotice $message */
36
        $message = self::getById(1, $database);
37
38
        return $message->getContent();
39
    }
40
41
    /**
42
     * Saves the object
43
     * @throws Exception
44
     */
45
    public function save()
46
    {
47
        if ($this->isNew()) {
48
            // insert
49
            throw new Exception('Not allowed to create new site notice object');
50
        }
51
        else {
52
            // update
53
            $statement = $this->dbObject->prepare(<<<SQL
54
UPDATE sitenotice
55
SET content = :content, updateversion = updateversion + 1
56
WHERE updateversion = :updateversion;
57
SQL
58
            );
59
            $statement->bindValue(':updateversion', $this->updateversion);
60
61
            $statement->bindValue(':content', $this->content);
62
63
            if (!$statement->execute()) {
64
                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

64
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
65
            }
66
67
            if ($statement->rowCount() !== 1) {
68
                throw new OptimisticLockFailedException();
69
            }
70
71
            $this->updateversion++;
72
        }
73
    }
74
75
    /**
76
     * Gets the content of the message
77
     * @return string
78
     */
79
    public function getContent()
80
    {
81
        return $this->content;
82
    }
83
84
    /**
85
     * Sets the content of the message
86
     *
87
     * @param string $content
88
     */
89
    public function setContent($content)
90
    {
91
        $this->content = $content;
92
    }
93
}
94