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 PDO; |
13
|
|
|
use Waca\DataObject; |
14
|
|
|
use Waca\PdoDatabase; |
15
|
|
|
|
16
|
|
|
class UserRole extends DataObject |
17
|
|
|
{ |
18
|
|
|
/** @var int */ |
19
|
|
|
private $user; |
20
|
|
|
/** @var string */ |
21
|
|
|
private $role; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param int $userId |
25
|
|
|
* @param PdoDatabase $database |
26
|
|
|
* |
27
|
|
|
* @return UserRole[] |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public static function getForUser($userId, PdoDatabase $database) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$sql = 'SELECT * FROM userrole WHERE user = :user'; |
32
|
|
|
$statement = $database->prepare($sql); |
33
|
|
|
$statement->bindValue(':user', $userId); |
34
|
|
|
|
35
|
|
|
$statement->execute(); |
36
|
|
|
|
37
|
|
|
$result = array(); |
38
|
|
|
|
39
|
|
|
/** @var Ban $v */ |
40
|
|
|
foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
41
|
|
|
$v->setDatabase($database); |
42
|
|
|
$result[] = $v; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Saves a data object to the database, either updating or inserting a record. |
50
|
|
|
* |
51
|
|
|
* @throws Exception |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function save() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if ($this->isNew()) { |
56
|
|
|
// insert |
57
|
|
|
$statement = $this->dbObject->prepare('INSERT INTO `userrole` (user, role) VALUES (:user, :role);' |
58
|
|
|
); |
59
|
|
|
$statement->bindValue(":user", $this->user); |
60
|
|
|
$statement->bindValue(":role", $this->role); |
61
|
|
|
|
62
|
|
|
if ($statement->execute()) { |
63
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
64
|
|
|
} |
65
|
|
|
else { |
66
|
|
|
throw new Exception($statement->errorInfo()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
else { |
70
|
|
|
// update |
71
|
|
|
throw new Exception('Updating roles is not available'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
#region Properties |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
public function getUser() |
81
|
|
|
{ |
82
|
|
|
return $this->user; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $user |
87
|
|
|
*/ |
88
|
|
|
public function setUser($user) |
89
|
|
|
{ |
90
|
|
|
$this->user = $user; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getRole() |
97
|
|
|
{ |
98
|
|
|
return $this->role; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $role |
103
|
|
|
*/ |
104
|
|
|
public function setRole($role) |
105
|
|
|
{ |
106
|
|
|
$this->role = $role; |
107
|
|
|
} |
108
|
|
|
#endregion |
109
|
|
|
} |
110
|
|
|
|
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.