Completed
Pull Request — master (#526)
by Michael
02:11
created

WelcomeTemplate::setUserCode()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Welcome template data object
5
 */
6
class WelcomeTemplate extends DataObject
7
{
8
	private $usercode;
9
	private $botcode;
10
11
	private $usageCache;
12
13
	/**
14
	 * Summary of getAll
15
	 * @param PdoDatabase $database
16
	 * @return WelcomeTemplate[]
17
	 */
18
	public static function getAll(PdoDatabase $database = null)
19
	{
20
		if ($database == null) {
21
			$database = gGetDb();
22
		}
23
24
		$statement = $database->prepare("SELECT * FROM welcometemplate;");
25
26
		$statement->execute();
27
28
		$result = array();
29
		/** @var WelcomeTemplate $v */
30
		foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
31
			$v->isNew = false;
32
			$v->setDatabase($database);
33
			$result[] = $v;
34
		}
35
36
		return $result;
37
	}
38
39
	public function save()
40
	{
41
		if ($this->isNew) {
42
// insert
43
			$statement = $this->dbObject->prepare("INSERT INTO welcometemplate (usercode, botcode) VALUES (:usercode, :botcode);");
44
			$statement->bindValue(":usercode", $this->usercode);
45
			$statement->bindValue(":botcode", $this->botcode);
46
47
			if ($statement->execute()) {
48
				$this->isNew = false;
49
				$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...
50
			}
51
			else {
52
				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

52
				throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
53
			}
54
		}
55
		else {
56
// update
57
			$statement = $this->dbObject->prepare("UPDATE `welcometemplate` SET usercode = :usercode, botcode = :botcode WHERE id = :id;");
58
			$statement->bindValue(":id", $this->id);
59
			$statement->bindValue(":usercode", $this->usercode);
60
			$statement->bindValue(":botcode", $this->botcode);
61
62
			if (!$statement->execute()) {
63
				throw new Exception($statement->errorInfo());
64
			}
65
		}
66
	}
67
68
	public function getUserCode()
69
	{
70
		return $this->usercode;
71
	}
72
73
	public function setUserCode($usercode)
74
	{
75
		$this->usercode = $usercode;
76
	}
77
78
	public function getBotCode()
79
	{
80
		return $this->botcode;
81
	}
82
83
	public function setBotCode($botcode)
84
	{
85
		$this->botcode = $botcode;
86
	}
87
88
	public function getUsersUsingTemplate()
89
	{
90
		if ($this->usageCache === null) {
91
			$statement = $this->dbObject->prepare("SELECT * FROM user WHERE welcome_template = :id;");
92
93
			$statement->execute(array(":id" => $this->id));
94
95
			$result = array();
96
			/** @var WelcomeTemplate $v */
97
			foreach ($statement->fetchAll(PDO::FETCH_CLASS, 'User') as $v) {
98
				$v->isNew = false;
99
				$v->setDatabase($this->dbObject);
100
				$result[] = $v;
101
			}
102
103
			$this->usageCache = $result;
104
		}
105
106
		return $this->usageCache;
107
	}
108
	
109
	public function getObjectDescription()
110
	{
111
		return '<a href="acc.php?action=templatemgmt&amp;view=' . $this->getId() . '">' . htmlentities($this->usercode) . "</a>";
112
	}
113
}
114