Passed
Pull Request — master (#526)
by Michael
06:45
created

GeoLocation::setData()   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 1
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
 * Geolocation data object
5
 *
6
 * This is a cache.
7
 */
8
class GeoLocation extends DataObject
9
{
10
	private $address;
11
	private $data;
12
	private $creation;
13
14
	/**
15
	 * @param string $address
16
	 * @param PdoDatabase $database
17
	 * @param bool $forUpdate
18
	 * @return GeoLocation
19
	 */
20
	public static function getByAddress($address, PdoDatabase $database, $forUpdate = false)
21
	{
22
		$lockMode = $forUpdate ? ' FOR UPDATE' : '';
23
		$sql = "SELECT * FROM geolocation WHERE address = :id LIMIT 1" . $lockMode;
24
25
		$statement = $database->prepare($sql);
26
		$statement->bindValue(":id", $address);
27
28
		$statement->execute();
29
30
		$resultObject = $statement->fetchObject(get_called_class());
31
32
		if ($resultObject != false) {
33
			$resultObject->isNew = false;
34
			$resultObject->setDatabase($database);
35
		}
36
37
		return $resultObject;
38
	}
39
40
	public function save()
41
	{
42
		if ($this->isNew) {
43
// insert
44
			$statement = $this->dbObject->prepare("INSERT INTO `geolocation` (address, data) VALUES (:address, :data) ON DUPLICATE KEY UPDATE address = address;");
45
			$statement->bindValue(":address", $this->address);
46
			$statement->bindValue(":data", $this->data);
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 `geolocation` SET address = :address, data = :data WHERE id = :id;");
58
			$statement->bindValue(":address", $this->address);
59
			$statement->bindValue(":id", $this->id);
60
			$statement->bindValue(":data", $this->data);
61
62
			if (!$statement->execute()) {
63
				throw new Exception($statement->errorInfo());
64
			}
65
		}
66
	}
67
68
	public function getAddress()
69
	{
70
		return $this->address;
71
	}
72
73
	/**
74
	 * @param string $address
75
	 */
76
	public function setAddress($address)
77
	{
78
		$this->address = $address;
79
	}
80
81
	public function getData()
82
	{
83
		return unserialize($this->data);
84
	}
85
86
	public function setData($data)
87
	{
88
		$this->data = serialize($data);
89
	}
90
91
	public function getCreation()
92
	{
93
		return $this->creation;
94
	}
95
}
96