enwikipedia-acc /
waca
| 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 | |||
| 17 | class Credential extends DataObject |
||
| 18 | { |
||
| 19 | /** @var int */ |
||
| 20 | private $user; |
||
| 21 | /** @var int */ |
||
| 22 | private $factor; |
||
| 23 | /** @var string */ |
||
| 24 | private $type; |
||
| 25 | /** @var string */ |
||
| 26 | private $data; |
||
| 27 | /** @var int */ |
||
| 28 | private $version; |
||
| 29 | private $timeout; |
||
| 30 | /** @var int */ |
||
| 31 | private $disabled = 0; |
||
| 32 | /** @var int */ |
||
| 33 | private $priority; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return int |
||
| 37 | */ |
||
| 38 | public function getUserId() |
||
| 39 | { |
||
| 40 | return $this->user; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param int $user |
||
| 45 | */ |
||
| 46 | public function setUserId($user) |
||
| 47 | { |
||
| 48 | $this->user = $user; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return int |
||
| 53 | */ |
||
| 54 | public function getFactor() |
||
| 55 | { |
||
| 56 | return $this->factor; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param int $factor |
||
| 61 | */ |
||
| 62 | public function setFactor($factor) |
||
| 63 | { |
||
| 64 | $this->factor = $factor; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getType() |
||
| 71 | { |
||
| 72 | return $this->type; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $type |
||
| 77 | */ |
||
| 78 | public function setType($type) |
||
| 79 | { |
||
| 80 | $this->type = $type; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getData() |
||
| 87 | { |
||
| 88 | return $this->data; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $data |
||
| 93 | */ |
||
| 94 | public function setData($data) |
||
| 95 | { |
||
| 96 | $this->data = $data; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return int |
||
| 101 | */ |
||
| 102 | public function getVersion() |
||
| 103 | { |
||
| 104 | return $this->version; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param int $version |
||
| 109 | */ |
||
| 110 | public function setVersion($version) |
||
| 111 | { |
||
| 112 | $this->version = $version; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | public function getTimeout() |
||
| 119 | { |
||
| 120 | if ($this->timeout === null) { |
||
| 121 | return null; |
||
| 122 | } |
||
| 123 | |||
| 124 | return new DateTimeImmutable($this->timeout); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param mixed $timeout |
||
| 129 | */ |
||
| 130 | public function setTimeout(DateTimeImmutable $timeout = null) |
||
| 131 | { |
||
| 132 | if ($timeout === null) { |
||
| 133 | $this->timeout = null; |
||
| 134 | } |
||
| 135 | else { |
||
| 136 | $this->timeout = $timeout->format('Y-m-d H:i:s'); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function getDisabled() |
||
| 144 | { |
||
| 145 | return $this->disabled; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param int $disabled |
||
| 150 | */ |
||
| 151 | public function setDisabled($disabled) |
||
| 152 | { |
||
| 153 | $this->disabled = $disabled; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | public function getPriority() |
||
| 160 | { |
||
| 161 | return $this->priority; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param int $priority |
||
| 166 | */ |
||
| 167 | public function setPriority($priority) |
||
| 168 | { |
||
| 169 | $this->priority = $priority; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function save() |
||
| 173 | { |
||
| 174 | if ($this->isNew()) { |
||
| 175 | // insert |
||
| 176 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 177 | INSERT INTO credential ( updateversion, user, factor, type, data, version, timeout, disabled, priority ) |
||
| 178 | VALUES ( 0, :user, :factor, :type, :data, :version, :timeout, :disabled, :priority ); |
||
| 179 | SQL |
||
| 180 | ); |
||
| 181 | $statement->bindValue(":user", $this->user); |
||
| 182 | $statement->bindValue(":factor", $this->factor); |
||
| 183 | $statement->bindValue(":type", $this->type); |
||
| 184 | $statement->bindValue(":data", $this->data); |
||
| 185 | $statement->bindValue(":version", $this->version); |
||
| 186 | $statement->bindValue(":timeout", $this->timeout); |
||
| 187 | $statement->bindValue(":disabled", $this->disabled); |
||
| 188 | $statement->bindValue(":priority", $this->priority); |
||
| 189 | |||
| 190 | if ($statement->execute()) { |
||
| 191 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
| 192 | } |
||
| 193 | else { |
||
| 194 | throw new Exception($statement->errorInfo()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 195 | } |
||
| 196 | } |
||
| 197 | else { |
||
| 198 | // update |
||
| 199 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 200 | UPDATE credential |
||
| 201 | SET factor = :factor |
||
| 202 | , data = :data |
||
| 203 | , version = :version |
||
| 204 | , timeout = :timeout |
||
| 205 | , disabled = :disabled |
||
| 206 | , priority = :priority |
||
| 207 | , updateversion = updateversion + 1 |
||
| 208 | WHERE id = :id AND updateversion = :updateversion; |
||
| 209 | SQL |
||
| 210 | ); |
||
| 211 | |||
| 212 | $statement->bindValue(':id', $this->id); |
||
| 213 | $statement->bindValue(':updateversion', $this->updateversion); |
||
| 214 | |||
| 215 | $statement->bindValue(":factor", $this->factor); |
||
| 216 | $statement->bindValue(":data", $this->data); |
||
| 217 | $statement->bindValue(":version", $this->version); |
||
| 218 | $statement->bindValue(":timeout", $this->timeout); |
||
| 219 | $statement->bindValue(":disabled", $this->disabled); |
||
| 220 | $statement->bindValue(":priority", $this->priority); |
||
| 221 | |||
| 222 | if (!$statement->execute()) { |
||
| 223 | throw new Exception($statement->errorInfo()); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ($statement->rowCount() !== 1) { |
||
| 227 | throw new OptimisticLockFailedException(); |
||
| 228 | } |
||
| 229 | |||
| 230 | $this->updateversion++; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |