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

AntiSpoofCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 74
ccs 0
cts 51
cp 0
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 3 1
A getByUsername() 0 21 2
A getUsername() 0 3 1
A setData() 0 3 1
A save() 0 17 3
A getTimestamp() 0 3 1
A setUsername() 0 3 1
1
<?php
2
3
/**
4
 * AntiSpoofCache data object
5
 */
6
class AntiSpoofCache extends DataObject
7
{
8
	protected $username;
9
	protected $data;
10
	protected $timestamp;
11
12
	public static function getByUsername($username, PdoDatabase $database)
13
	{
14
		$statement = $database->prepare(<<<SQL
15
SELECT *
16
FROM antispoofcache
17
WHERE username = :id AND timestamp > date_sub(now(), interval 3 hour)
18
LIMIT 1
19
SQL
20
		);
21
		$statement->bindValue(":id", $username);
22
23
		$statement->execute();
24
25
		$resultObject = $statement->fetchObject(get_called_class());
26
27
		if ($resultObject != false) {
28
			$resultObject->isNew = false;
29
			$resultObject->setDatabase($database);
30
		}
31
32
		return $resultObject;
33
	}
34
35
	public function getUsername()
36
	{
37
		return $this->username;
38
	}
39
40
	public function setUsername($username)
41
	{
42
		$this->username = $username;
43
	}
44
45
	public function getData()
46
	{
47
		return $this->data;
48
	}
49
50
	/**
51
	 * @param string $data
52
	 */
53
	public function setData($data)
54
	{
55
		$this->data = $data;
56
	}
57
58
	public function getTimestamp()
59
	{
60
		return $this->timestamp;
61
	}
62
63
	public function save()
64
	{
65
		if ($this->isNew) {
66
			// insert
67
			// clear old data first
68
			$this->dbObject->exec("delete from antispoofcache where timestamp < date_sub(now(), interval 3 hour);");
69
70
			$statement = $this->dbObject->prepare("INSERT INTO antispoofcache (username, data) VALUES (:username, :data);");
71
			$statement->bindValue(":username", $this->username);
72
			$statement->bindValue(":data", $this->data);
73
74
			if ($statement->execute()) {
75
				$this->isNew = false;
76
				$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...
77
			}
78
			else {
79
				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

79
				throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
80
			}
81
		}
82
	}
83
}
84