Completed
Pull Request — master (#526)
by Michael
16:45 queued 06:57
created

RDnsCache::getAddress()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * rDNS Cache data object
5
 */
6
class RDnsCache extends DataObject
7
{
8
	private $address;
9
	private $data;
10
	private $creation;
11
12
	/**
13
	 * @param string $address
14
	 * @param PdoDatabase $database
15
	 * @return RDnsCache
16
	 */
17
	public static function getByAddress($address, PdoDatabase $database)
18
	{
19
		$statement = $database->prepare("SELECT * FROM rdnscache WHERE address = :id LIMIT 1;");
20
		$statement->bindValue(":id", $address);
21
22
		$statement->execute();
23
24
		$resultObject = $statement->fetchObject(get_called_class());
25
26
		if ($resultObject != false) {
27
			$resultObject->isNew = false;
28
			$resultObject->setDatabase($database);
29
		}
30
31
		return $resultObject;
32
	}
33
34
	public function save()
35
	{
36
		if ($this->isNew) {
37
// insert
38
			$statement = $this->dbObject->prepare("INSERT INTO `rdnscache` (address, data) VALUES (:address, :data);");
39
			$statement->bindValue(":address", $this->address);
40
			$statement->bindValue(":data", $this->data);
41
			if ($statement->execute()) {
42
				$this->isNew = false;
43
				$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...
44
			}
45
			else {
46
				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

46
				throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
47
			}
48
		}
49
		else {
50
// update
51
			$statement = $this->dbObject->prepare("UPDATE `rdnscache` SET address = :address, data = :data WHERE id = :id;");
52
			$statement->bindValue(":address", $this->address);
53
			$statement->bindValue(":id", $this->id);
54
			$statement->bindValue(":data", $this->data);
55
56
			if (!$statement->execute()) {
57
				throw new Exception($statement->errorInfo());
58
			}
59
		}
60
	}
61
62
	public function getAddress()
63
	{
64
		return $this->address;
65
	}
66
67
	/**
68
	 * @param string $address
69
	 */
70
	public function setAddress($address)
71
	{
72
		$this->address = $address;
73
	}
74
75
	public function getData()
76
	{
77
		return unserialize($this->data);
78
	}
79
80
	public function setData($data)
81
	{
82
		$this->data = serialize($data);
83
	}
84
85
	public function getCreation()
86
	{
87
		return $this->creation;
88
	}
89
}
90