Completed
Pull Request — master (#526)
by Michael
01:57
created

InterfaceMessage   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 114
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setContent() 0 3 1
A getContent() 0 3 1
A getDescription() 0 3 1
A getUpdateCounter() 0 3 1
A getContentForDisplay() 0 12 2
A save() 0 26 4
A getObjectDescription() 0 3 1
A getType() 0 3 1
A setType() 0 3 1
A get() 0 5 1
A setDescription() 0 3 1
1
<?php
2
3
/**
4
 * Interface data object
5
 *
6
 * Interface messages for those messages which are not templates.
7
 */
8
class InterfaceMessage extends DataObject
9
{
10
	private $content;
11
	private $updatecounter;
12
	private $description;
13
	private $type;
14
15
	const SITENOTICE = '31';
16
	const DECL_BANNED = '19';
17
18
	/**
19
	 * Get a message.
20
	 *
21
	 * This is going to be used as a new way of dealing with saved messages for #28
22
	 *
23
	 * The basic idea is there's a key stored in a new column, and we do lookups on that
24
	 * instead of a possibly variable auto-incrementing ID.
25
	 *
26
	 * We can use class constants so the keys are defined in one place only for now, and for
27
	 * now we are using the auto-incrementing ID as the value of the key, so this function
28
	 * just uses getById() at the moment.
29
	 *
30
	 * @param mixed $key
31
	 * @return mixed
32
	 */
33
	public static function get($key)
34
	{
35
		/** @var InterfaceMessage $message */
36
		$message = self::getById($key, gGetDb());
37
		return $message->getContentForDisplay();
38
	}
39
40
	public function save()
41
	{
42
		if ($this->isNew) {
43
// insert
44
			$statement = $this->dbObject->prepare("INSERT INTO interfacemessage (updatecounter, description, type, content) VALUES (0, :desc, :type, :content);");
45
			$statement->bindValue(":type", $this->type);
46
			$statement->bindValue(":desc", $this->description);
47
			$statement->bindValue(":content", $this->content);
48
			if ($statement->execute()) {
49
				$this->isNew = false;
50
				$this->id = $this->dbObject->lastInsertId();
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $this->dbObject->lastInsertId() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
51
			}
52
			else {
53
				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

53
				throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
54
			}
55
		}
56
		else {
57
// update
58
			$statement = $this->dbObject->prepare("UPDATE interfacemessage SET type = :type, description = :desc, content = :content, updatecounter = updatecounter + 1 WHERE id = :id;");
59
			$statement->bindValue(":id", $this->id);
60
			$statement->bindValue(":type", $this->type);
61
			$statement->bindValue(":desc", $this->description);
62
			$statement->bindValue(":content", $this->content);
63
64
			if (!$statement->execute()) {
65
				throw new Exception($statement->errorInfo());
66
			}
67
		}
68
	}
69
70
	public function getContent()
71
	{
72
		return $this->content;
73
	}
74
75
	public function getContentForDisplay()
76
	{
77
		global $baseurl;
78
79
		$message = $this->content;
80
81
		if (strpos($message, "%VERSION%") !== false) {
82
			$message = str_replace('%VERSION%', Environment::getToolVersion(), $message);
83
		}
84
85
		$message = str_replace('%TSURL%', $baseurl, $message);
86
		return $message;
87
	}
88
89
	public function setContent($content)
90
	{
91
		$this->content = $content;
92
	}
93
94
	public function getUpdateCounter()
95
	{
96
		return $this->updatecounter;
97
	}
98
99
	public function getDescription()
100
	{
101
		return $this->description;
102
	}
103
104
	public function setDescription($description)
105
	{
106
		$this->description = $description;
107
	}
108
109
	public function getType()
110
	{
111
		return $this->type;
112
	}
113
114
	public function setType($type)
115
	{
116
		$this->type = $type;
117
	}
118
	
119
	public function getObjectDescription()
120
	{
121
		return '<a href="acc.php?action=messagemgmt&amp;view=' . $this->getId() . '">' . htmlentities($this->description) . "</a>";
122
	}
123
}
124