Failed Conditions
Branch newinternal (104de7)
by Simon
09:33
created

Ban::getBanByTarget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 23
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 23
loc 23
rs 9.0856
cc 2
eloc 12
nc 2
nop 3
1
<?php
2
namespace Waca\DataObjects;
3
4
use Exception;
5
use PDO;
6
use Waca\DataObject;
7
use Waca\Exceptions\OptimisticLockFailedException;
8
use Waca\PdoDatabase;
9
10
/**
11
 * Ban data object
12
 */
13
class Ban extends DataObject
14
{
15
	private $type;
16
	private $target;
17
	private $user;
18
	private $reason;
19
	private $date;
20
	private $duration;
21
	private $active;
22
23
	/**
24
	 * Gets all active bans, filtered by the optional target.
25
	 *
26
	 * @param string|null $target
27
	 * @param PdoDatabase $database
28
	 *
29
	 * @return Ban[]
30
	 */
31
	public static function getActiveBans($target, PdoDatabase $database)
32
	{
33
		if ($target !== null) {
34
			$query = <<<SQL
35
SELECT * FROM ban WHERE target = :target AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;
36
SQL;
37
			$statement = $database->prepare($query);
38
			$statement->bindValue(":target", $target);
39
		}
40
		else {
41
			$query = "SELECT * FROM ban WHERE (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;";
42
			$statement = $database->prepare($query);
43
		}
44
45
		$statement->execute();
46
47
		$result = array();
48
49
		/** @var Ban $v */
50
		foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
51
			$v->setDatabase($database);
52
			$result[] = $v;
53
		}
54
55
		return $result;
56
	}
57
58
	/**
59
	 * Gets a ban by it's ID if it's currently active.
60
	 *
61
	 * @param     integer $id
62
	 * @param PdoDatabase $database
63
	 *
64
	 * @return Ban
65
	 */
66
	public static function getActiveId($id, PdoDatabase $database)
67
	{
68
		$statement = $database->prepare(<<<SQL
69
SELECT *
70
FROM ban
71
WHERE id = :id  AND (duration > UNIX_TIMESTAMP() OR duration = -1) AND active = 1;
72
SQL
73
		);
74
		$statement->bindValue(":id", $id);
75
76
		$statement->execute();
77
78
		$resultObject = $statement->fetchObject(get_called_class());
79
80
		if ($resultObject != false) {
81
			$resultObject->setDatabase($database);
82
		}
83
84
		return $resultObject;
85
	}
86
87
	/**
88
	 * Get all active bans for a target and type.
89
	 *
90
	 * @param string      $target
91
	 * @param string      $type
92
	 * @param PdoDatabase $database
93
	 *
94
	 * @return Ban
95
	 */
96 View Code Duplication
	public static function getBanByTarget($target, $type, PdoDatabase $database)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
	{
98
		$query = <<<SQL
99
SELECT * FROM ban
100
WHERE type = :type
101
	AND target = :target
102
	AND (duration > UNIX_TIMESTAMP() OR duration = -1)
103
	AND active = 1;
104
SQL;
105
		$statement = $database->prepare($query);
106
		$statement->bindValue(":target", $target);
107
		$statement->bindValue(":type", $type);
108
109
		$statement->execute();
110
111
		$resultObject = $statement->fetchObject(get_called_class());
112
113
		if ($resultObject != false) {
114
			$resultObject->setDatabase($database);
115
		}
116
117
		return $resultObject;
118
	}
119
120
	/**
121
	 * @throws Exception
122
	 */
123
	public function save()
124
	{
125
		if ($this->isNew()) {
126
			// insert
127
			$statement = $this->dbObject->prepare(<<<SQL
128
INSERT INTO `ban` (type, target, user, reason, date, duration, active)
129
VALUES (:type, :target, :user, :reason, CURRENT_TIMESTAMP(), :duration, :active);
130
SQL
131
			);
132
			$statement->bindValue(":type", $this->type);
133
			$statement->bindValue(":target", $this->target);
134
			$statement->bindValue(":user", $this->user);
135
			$statement->bindValue(":reason", $this->reason);
136
			$statement->bindValue(":duration", $this->duration);
137
			$statement->bindValue(":active", $this->active);
138
139
			if ($statement->execute()) {
140
				$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...
141
			}
142
			else {
143
				throw new Exception($statement->errorInfo());
144
			}
145
		}
146
		else {
147
			// update
148
			$statement = $this->dbObject->prepare(<<<SQL
149
UPDATE `ban`
150
SET duration = :duration, active = :active, user = :user, updateversion = updateversion + 1
151
WHERE id = :id AND updateversion = :updateversion
152
LIMIT 1;
153
SQL
154
			);
155
			$statement->bindValue(':id', $this->id);
156
			$statement->bindValue(':updateversion', $this->updateversion);
157
158
			$statement->bindValue(':duration', $this->duration);
159
			$statement->bindValue(':active', $this->active);
160
			$statement->bindValue(':user', $this->user);
161
162
			if (!$statement->execute()) {
163
				throw new Exception($statement->errorInfo());
164
			}
165
166
			if($statement->rowCount() !== 1){
167
				throw new OptimisticLockFailedException();
168
			}
169
170
			$this->updateversion++;
171
		}
172
	}
173
174
	/**
175
	 * @return string
176
	 */
177
	public function getType()
178
	{
179
		return $this->type;
180
	}
181
182
	/**
183
	 * @param string $type
184
	 */
185
	public function setType($type)
186
	{
187
		$this->type = $type;
188
	}
189
190
	/**
191
	 * @return string
192
	 */
193
	public function getTarget()
194
	{
195
		return $this->target;
196
	}
197
198
	/**
199
	 * @param string $target
200
	 */
201
	public function setTarget($target)
202
	{
203
		$this->target = $target;
204
	}
205
206
	/**
207
	 * @return string
208
	 */
209
	public function getReason()
210
	{
211
		return $this->reason;
212
	}
213
214
	/**
215
	 * @param string $reason
216
	 */
217
	public function setReason($reason)
218
	{
219
		$this->reason = $reason;
220
	}
221
222
	/**
223
	 * @return mixed
224
	 */
225
	public function getDate()
226
	{
227
		return $this->date;
228
	}
229
230
	/**
231
	 * @return mixed
232
	 */
233
	public function getDuration()
234
	{
235
		return $this->duration;
236
	}
237
238
	/**
239
	 * @param mixed $duration
240
	 */
241
	public function setDuration($duration)
242
	{
243
		$this->duration = $duration;
244
	}
245
246
	/**
247
	 * @return bool
248
	 */
249
	public function isActive()
250
	{
251
		return $this->active == 1;
252
	}
253
254
	/**
255
	 * @param bool $active
256
	 */
257
	public function setActive($active)
258
	{
259
		$this->active = $active ? 1 : 0;
260
	}
261
262
	/**
263
	 * @return int
264
	 */
265
	public function getUser()
266
	{
267
		return $this->user;
268
	}
269
270
	/**
271
	 * @param int $user UserID of user who is setting the ban
272
	 *
273
	 * @throws Exception
274
	 */
275
	public function setUser($user)
276
	{
277
		$this->user = $user;
278
	}
279
}
280