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 Exception; |
||
13 | use Waca\DataObject; |
||
14 | use Waca\Exceptions\OptimisticLockFailedException; |
||
15 | |||
16 | class OAuthToken extends DataObject |
||
17 | { |
||
18 | /** @var int */ |
||
19 | private $user; |
||
20 | /** @var string */ |
||
21 | private $token; |
||
22 | /** @var string */ |
||
23 | private $secret; |
||
24 | /** @var string */ |
||
25 | private $type; |
||
26 | /** @var string */ |
||
27 | private $expiry; |
||
28 | |||
29 | public function save() |
||
30 | { |
||
31 | if ($this->isNew()) { |
||
32 | // insert |
||
33 | $statement = $this->dbObject->prepare(<<<SQL |
||
34 | INSERT INTO oauthtoken ( user, token, secret, type, expiry ) |
||
35 | VALUES ( :user, :token, :secret, :type, :expiry ); |
||
36 | SQL |
||
37 | ); |
||
38 | $statement->bindValue(":user", $this->user); |
||
39 | $statement->bindValue(":token", $this->token); |
||
40 | $statement->bindValue(":secret", $this->secret); |
||
41 | $statement->bindValue(":type", $this->type); |
||
42 | $statement->bindValue(":expiry", $this->expiry); |
||
43 | |||
44 | if ($statement->execute()) { |
||
45 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
46 | } |
||
47 | else { |
||
48 | throw new Exception($statement->errorInfo()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
49 | } |
||
50 | } |
||
51 | else { |
||
52 | // update |
||
53 | $statement = $this->dbObject->prepare(<<<SQL |
||
54 | UPDATE oauthtoken |
||
55 | SET token = :token |
||
56 | , secret = :secret |
||
57 | , type = :type |
||
58 | , expiry = :expiry |
||
59 | , updateversion = updateversion + 1 |
||
60 | WHERE id = :id AND updateversion = :updateversion; |
||
61 | SQL |
||
62 | ); |
||
63 | |||
64 | $statement->bindValue(':id', $this->id); |
||
65 | $statement->bindValue(':updateversion', $this->updateversion); |
||
66 | |||
67 | $statement->bindValue(":token", $this->token); |
||
68 | $statement->bindValue(":secret", $this->secret); |
||
69 | $statement->bindValue(":type", $this->type); |
||
70 | $statement->bindValue(":expiry", $this->expiry); |
||
71 | |||
72 | if (!$statement->execute()) { |
||
73 | throw new Exception($statement->errorInfo()); |
||
74 | } |
||
75 | |||
76 | if ($statement->rowCount() !== 1) { |
||
77 | throw new OptimisticLockFailedException(); |
||
78 | } |
||
79 | |||
80 | $this->updateversion++; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | #region properties |
||
85 | |||
86 | /** |
||
87 | * @return mixed |
||
88 | */ |
||
89 | public function getUserId() |
||
90 | { |
||
91 | return $this->user; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param mixed $user |
||
96 | */ |
||
97 | public function setUserId($user) |
||
98 | { |
||
99 | $this->user = $user; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function getToken() |
||
106 | { |
||
107 | return $this->token; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param mixed $token |
||
112 | */ |
||
113 | public function setToken($token) |
||
114 | { |
||
115 | $this->token = $token; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function getSecret() |
||
122 | { |
||
123 | return $this->secret; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param mixed $secret |
||
128 | */ |
||
129 | public function setSecret($secret) |
||
130 | { |
||
131 | $this->secret = $secret; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return mixed |
||
136 | */ |
||
137 | public function getType() |
||
138 | { |
||
139 | return $this->type; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param mixed $type |
||
144 | */ |
||
145 | public function setType($type) |
||
146 | { |
||
147 | $this->type = $type; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getExpiry() |
||
154 | { |
||
155 | return $this->expiry; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param string $expiry |
||
160 | */ |
||
161 | public function setExpiry($expiry) |
||
162 | { |
||
163 | $this->expiry = $expiry; |
||
164 | } |
||
165 | #endregion |
||
166 | |||
167 | } |