1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\DataObjects; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Waca\DataObject; |
13
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
14
|
|
|
|
15
|
|
View Code Duplication |
class OAuthToken extends DataObject |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** @var int */ |
18
|
|
|
private $user; |
19
|
|
|
/** @var string */ |
20
|
|
|
private $token; |
21
|
|
|
/** @var string */ |
22
|
|
|
private $secret; |
23
|
|
|
/** @var string */ |
24
|
|
|
private $type; |
25
|
|
|
/** @var string */ |
26
|
|
|
private $expiry; |
27
|
|
|
|
28
|
|
|
public function save() |
29
|
|
|
{ |
30
|
|
|
if ($this->isNew()) { |
31
|
|
|
// insert |
32
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
33
|
|
|
INSERT INTO oauthtoken ( user, token, secret, type, expiry ) |
34
|
|
|
VALUES ( :user, :token, :secret, :type, :expiry ); |
35
|
|
|
SQL |
36
|
|
|
); |
37
|
|
|
$statement->bindValue(":user", $this->user); |
38
|
|
|
$statement->bindValue(":token", $this->token); |
39
|
|
|
$statement->bindValue(":secret", $this->secret); |
40
|
|
|
$statement->bindValue(":type", $this->type); |
41
|
|
|
$statement->bindValue(":expiry", $this->expiry); |
42
|
|
|
|
43
|
|
|
if ($statement->execute()) { |
44
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
45
|
|
|
} |
46
|
|
|
else { |
47
|
|
|
throw new Exception($statement->errorInfo()); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
else { |
51
|
|
|
// update |
52
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
53
|
|
|
UPDATE oauthtoken |
54
|
|
|
SET token = :token |
55
|
|
|
, secret = :secret |
56
|
|
|
, type = :type |
57
|
|
|
, expiry = :expiry |
58
|
|
|
, updateversion = updateversion + 1 |
59
|
|
|
WHERE id = :id AND updateversion = :updateversion; |
60
|
|
|
SQL |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$statement->bindValue(':id', $this->id); |
64
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
65
|
|
|
|
66
|
|
|
$statement->bindValue(":token", $this->token); |
67
|
|
|
$statement->bindValue(":secret", $this->secret); |
68
|
|
|
$statement->bindValue(":type", $this->type); |
69
|
|
|
$statement->bindValue(":expiry", $this->expiry); |
70
|
|
|
|
71
|
|
|
if (!$statement->execute()) { |
72
|
|
|
throw new Exception($statement->errorInfo()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($statement->rowCount() !== 1) { |
76
|
|
|
throw new OptimisticLockFailedException(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->updateversion++; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
#region properties |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return mixed |
|
|
|
|
87
|
|
|
*/ |
88
|
|
|
public function getUserId() |
89
|
|
|
{ |
90
|
|
|
return $this->user; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $user |
95
|
|
|
*/ |
96
|
|
|
public function setUserId($user) |
97
|
|
|
{ |
98
|
|
|
$this->user = $user; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return mixed |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
public function getToken() |
105
|
|
|
{ |
106
|
|
|
return $this->token; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param mixed $token |
111
|
|
|
*/ |
112
|
|
|
public function setToken($token) |
113
|
|
|
{ |
114
|
|
|
$this->token = $token; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return mixed |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
public function getSecret() |
121
|
|
|
{ |
122
|
|
|
return $this->secret; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param mixed $secret |
127
|
|
|
*/ |
128
|
|
|
public function setSecret($secret) |
129
|
|
|
{ |
130
|
|
|
$this->secret = $secret; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return mixed |
|
|
|
|
135
|
|
|
*/ |
136
|
|
|
public function getType() |
137
|
|
|
{ |
138
|
|
|
return $this->type; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param mixed $type |
143
|
|
|
*/ |
144
|
|
|
public function setType($type) |
145
|
|
|
{ |
146
|
|
|
$this->type = $type; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getExpiry() |
153
|
|
|
{ |
154
|
|
|
return $this->expiry; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $expiry |
159
|
|
|
*/ |
160
|
|
|
public function setExpiry($expiry) |
161
|
|
|
{ |
162
|
|
|
$this->expiry = $expiry; |
163
|
|
|
} |
164
|
|
|
#endregion |
165
|
|
|
|
166
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.