1 | <?php |
||
2 | /****************************************************************************** |
||
3 | * Wikipedia Account Creation Assistance tool * |
||
4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
5 | * * |
||
6 | * This is free and unencumbered software released into the public domain. * |
||
7 | * Please see LICENSE.md for the full licencing statement. * |
||
8 | ******************************************************************************/ |
||
9 | |||
10 | namespace Waca\DataObjects; |
||
11 | |||
12 | use DateTimeImmutable; |
||
13 | use Exception; |
||
14 | use Waca\DataObject; |
||
15 | use Waca\Exceptions\OptimisticLockFailedException; |
||
16 | use Waca\PdoDatabase; |
||
17 | |||
18 | /** |
||
19 | * rDNS Cache data object |
||
20 | */ |
||
21 | class RDnsCache extends DataObject |
||
22 | { |
||
23 | private $address; |
||
24 | private $data; |
||
25 | private $creation; |
||
26 | |||
27 | /** |
||
28 | * @param string $address |
||
29 | * @param PdoDatabase $database |
||
30 | * |
||
31 | * @return RDnsCache|false |
||
32 | */ |
||
33 | public static function getByAddress($address, PdoDatabase $database) |
||
34 | { |
||
35 | // @todo add cache invalidation (timestamp?) |
||
36 | $statement = $database->prepare("SELECT * FROM rdnscache WHERE address = :id LIMIT 1;"); |
||
37 | $statement->bindValue(":id", $address); |
||
38 | |||
39 | $statement->execute(); |
||
40 | |||
41 | $resultObject = $statement->fetchObject(get_called_class()); |
||
42 | |||
43 | if ($resultObject != false) { |
||
44 | $resultObject->setDatabase($database); |
||
45 | } |
||
46 | |||
47 | return $resultObject; |
||
48 | } |
||
49 | |||
50 | public function save() |
||
51 | { |
||
52 | if ($this->isNew()) { |
||
53 | // insert |
||
54 | $statement = $this->dbObject->prepare(<<<SQL |
||
55 | INSERT INTO `rdnscache` (address, data) VALUES (:address, :data); |
||
56 | SQL |
||
57 | ); |
||
58 | $statement->bindValue(":address", $this->address); |
||
59 | $statement->bindValue(":data", $this->data); |
||
60 | |||
61 | if ($statement->execute()) { |
||
62 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
63 | } |
||
64 | else { |
||
65 | throw new Exception($statement->errorInfo()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
66 | } |
||
67 | } |
||
68 | else { |
||
69 | // update |
||
70 | $statement = $this->dbObject->prepare(<<<SQL |
||
71 | UPDATE `rdnscache` |
||
72 | SET address = :address, data = :data, updateversion = updateversion + 1 |
||
73 | WHERE id = :id AND updateversion = :updateversion; |
||
74 | SQL |
||
75 | ); |
||
76 | |||
77 | $statement->bindValue(':id', $this->id); |
||
78 | $statement->bindValue(':updateversion', $this->updateversion); |
||
79 | |||
80 | $statement->bindValue(':address', $this->address); |
||
81 | $statement->bindValue(':data', $this->data); |
||
82 | |||
83 | if (!$statement->execute()) { |
||
84 | throw new Exception($statement->errorInfo()); |
||
85 | } |
||
86 | |||
87 | if ($statement->rowCount() !== 1) { |
||
88 | throw new OptimisticLockFailedException(); |
||
89 | } |
||
90 | |||
91 | $this->updateversion++; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | public function getAddress() |
||
96 | { |
||
97 | return $this->address; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $address |
||
102 | */ |
||
103 | public function setAddress($address) |
||
104 | { |
||
105 | $this->address = $address; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getData() |
||
112 | { |
||
113 | return unserialize($this->data); |
||
114 | } |
||
115 | |||
116 | public function setData($data) |
||
117 | { |
||
118 | $this->data = serialize($data); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return DateTimeImmutable |
||
123 | */ |
||
124 | public function getCreation() |
||
125 | { |
||
126 | return new DateTimeImmutable($this->creation); |
||
127 | } |
||
128 | } |
||
129 |