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

Comment::getForRequest()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 34
ccs 0
cts 25
cp 0
crap 30
rs 9.3554
1
<?php
2
3
/**
4
 * Comment data object
5
 */
6
class Comment extends DataObject
7
{
8
	private $time;
9
	private $user;
10
	private $comment;
11
	private $visibility = "user";
12
	private $request;
13
14
	/**
15
	 * @param integer $id
16
	 * @param null|PdoDatabase $database
17
	 * @return Comment[]
18
	 * @throws Exception
19
	 */
20
	public static function getForRequest($id, PdoDatabase $database = null)
21
	{
22
		if ($database == null) {
23
			$database = gGetDb();
24
		}
25
26
		if (User::getCurrent()->isAdmin() || User::getCurrent()->isCheckuser()) {
27
			// current user is an admin or checkuser, so retrieve everything.
28
			$statement = $database->prepare("SELECT * FROM comment WHERE request = :target;");
29
		}
30
		else {
31
			// current user isn't an admin, so limit to only those which are visible to users, and private comments
32
			// the user has posted themselves.
33
			$statement = $database->prepare(<<<SQL
34
SELECT * FROM comment
35
WHERE request = :target AND (visibility = 'user' OR user = :userid);
36
SQL
37
);
38
			$statement->bindValue(":userid", User::getCurrent()->getId());
39
		}
40
41
		$statement->bindValue(":target", $id);
42
43
		$statement->execute();
44
45
		$result = array();
46
		/** @var Comment $v */
47
		foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
48
			$v->isNew = false;
49
			$v->setDatabase($database);
50
			$result[] = $v;
51
		}
52
53
		return $result;
54
	}
55
56
	public function save()
57
	{
58
		if ($this->isNew) {
59
			// insert
60
			$statement = $this->dbObject->prepare(<<<SQL
61
INSERT INTO comment ( time, user, comment, visibility, request )
62
VALUES ( CURRENT_TIMESTAMP(), :user, :comment, :visibility, :request );
63
SQL
64
			);
65
			$statement->bindValue(":user", $this->user);
66
			$statement->bindValue(":comment", $this->comment);
67
			$statement->bindValue(":visibility", $this->visibility);
68
			$statement->bindValue(":request", $this->request);
69
70
			if ($statement->execute()) {
71
				$this->isNew = false;
72
				$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...
73
			}
74
			else {
75
				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

75
				throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
76
			}
77
		}
78
		else {
79
			// update
80
			$statement = $this->dbObject->prepare(<<<SQL
81
UPDATE comment
82
SET comment = :comment, visibility = :visibility
83
WHERE id = :id;
84
SQL
85
			);
86
			$statement->bindValue(":id", $this->id);
87
			$statement->bindValue(":comment", $this->comment);
88
			$statement->bindValue(":visibility", $this->visibility);
89
90
			if (!$statement->execute()) {
91
				throw new Exception($statement->errorInfo());
92
			}
93
		}
94
	}
95
96
	public function getTime()
97
	{
98
		return $this->time;
99
	}
100
101
	public function getUser()
102
	{
103
		return $this->user;
104
	}
105
106
	/**
107
	 * Summary of getUserObject
108
	 * @return User|null
109
	 */
110
	public function getUserObject()
111
	{
112
		return User::getById($this->user, $this->dbObject);
113
	}
114
115
	public function setUser($user)
116
	{
117
		$this->user = $user;
118
	}
119
120
	public function getComment()
121
	{
122
		return $this->comment;
123
	}
124
125
	public function setComment($comment)
126
	{
127
		$this->comment = $comment;
128
	}
129
130
	public function getVisibility()
131
	{
132
		return $this->visibility;
133
	}
134
135
	public function setVisibility($visibility)
136
	{
137
		$this->visibility = $visibility;
138
	}
139
140
	public function getRequest()
141
	{
142
		return $this->request;
143
	}
144
145
	/**
146
	 * Summary of getRequestObject
147
	 * @return Request|null
148
	 */
149
	public function getRequestObject()
150
	{
151
		return Request::getById($this->request, $this->dbObject);
152
	}
153
154
	public function setRequest($request)
155
	{
156
		$this->request = $request;
157
	}
158
}
159