1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PRReviewWatcher\Entity; |
4
|
|
|
|
5
|
|
|
class CredentialRepository extends Repository |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Find every credential |
9
|
|
|
* |
10
|
|
|
* @return array |
11
|
|
|
*/ |
12
|
|
View Code Duplication |
public function findAll() |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$sql = 'SELECT * FROM credential ORDER BY idCred DESC'; |
15
|
|
|
$result = $this->db->prepare($sql); |
16
|
|
|
$result->execute(); |
17
|
|
|
$result = $result->fetchAll(); |
18
|
|
|
$credentials = array(); |
19
|
|
|
|
20
|
|
|
foreach ($result as $row) { |
21
|
|
|
$credentialId = $row['idCred']; |
22
|
|
|
$credentials[$credentialId] = $this->buildDomainObject($row); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $credentials; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param integer $id |
30
|
|
|
* |
31
|
|
|
* @return Credential |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function find($id) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$sql = 'SELECT * FROM credential WHERE idCred= :idCred'; |
36
|
|
|
$row = $this->db->prepare($sql); |
37
|
|
|
$row->bindValue(':idCred', $id, SQLITE3_TEXT); |
38
|
|
|
$row->execute(); |
39
|
|
|
$row = $this->db->fetchAssoc($sql, array($id)); |
40
|
|
|
|
41
|
|
|
return $this->buildDomainObject($row); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Save credential into db |
46
|
|
|
* |
47
|
|
|
* @param Credential $credential |
48
|
|
|
*/ |
49
|
|
|
public function save(Credential $credential) |
50
|
|
|
{ |
51
|
|
|
$credentialData = array( |
52
|
|
|
'nameCred' => $credential->getNameCred(), |
53
|
|
|
'token' => $credential->getToken(), |
54
|
|
|
); |
55
|
|
|
if ($credential->getIdCred()) { |
56
|
|
|
$this->getDb()->update('credential', $credentialData, array('idCred' => $credential->getIdCred())); |
57
|
|
|
} else { |
58
|
|
|
$this->getDb()->insert('credential', $credentialData); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $id |
64
|
|
|
*/ |
65
|
|
|
public function delete($id) |
66
|
|
|
{ |
67
|
|
|
$this->getDb()->delete('credential', array('idCred' => $id)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function findAllAsArray() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$sql = 'SELECT idCred, nameCred FROM credential ORDER BY idCred DESC'; |
76
|
|
|
$result = $this->db->prepare($sql); |
77
|
|
|
$result->execute(); |
78
|
|
|
$result = $result->fetchAll(); |
79
|
|
|
$credentials = array(); |
80
|
|
|
|
81
|
|
|
foreach ($result as $row) { |
82
|
|
|
$id = $row['idCred']; |
83
|
|
|
$credentials[$id] = $row['nameCred']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $credentials; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $repoHook |
91
|
|
|
* @param $branchHook |
92
|
|
|
* |
93
|
|
|
* @return \Doctrine\DBAL\Driver\Statement|mixed |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function findToken($repoHook, $branchHook) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if ($branchHook == null) { |
98
|
|
|
$sql = "SELECT token FROM credential AS c JOIN project AS P ON c.idcred = p.credential WHERE p.name = :name AND p.branch = 'all';"; |
99
|
|
|
$result = $this->db->prepare($sql); |
100
|
|
|
$result->bindValue(':name', $repoHook); |
101
|
|
|
} else { |
102
|
|
|
$sql = "SELECT token FROM credential AS c JOIN project AS P ON c.idcred = p.credential WHERE p.name = :name AND p.branch = :branch;"; |
103
|
|
|
$result = $this->db->prepare($sql); |
104
|
|
|
$result->bindValue(':name', $repoHook); |
105
|
|
|
$result->bindValue(':branch', $branchHook); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$result->execute(); |
109
|
|
|
$result = $result->fetch(); |
110
|
|
|
$result = $result['token']; |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $repoHook |
117
|
|
|
* |
118
|
|
|
* @return \Doctrine\DBAL\Driver\Statement|mixed |
119
|
|
|
*/ |
120
|
|
|
public function findNameCredential($repoHook) |
121
|
|
|
{ |
122
|
|
|
$sql = 'SELECT nameCred FROM credential AS c JOIN project AS p ON c.idCred = p.credential AND p.name = :name;'; |
123
|
|
|
$result = $this->db->prepare($sql); |
124
|
|
|
$result->bindValue(':name', $repoHook); |
125
|
|
|
$result->execute(); |
126
|
|
|
$result = $result->fetch(); |
127
|
|
|
$result = $result['nameCred']; |
128
|
|
|
|
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $row |
134
|
|
|
* @return Credential |
135
|
|
|
*/ |
136
|
|
|
protected function buildDomainObject($row) |
137
|
|
|
{ |
138
|
|
|
$credential = new Credential(); |
139
|
|
|
$credential->setIdCred($row['idCred']); |
140
|
|
|
$credential->setNameCred($row['nameCred']); |
141
|
|
|
$credential->setToken($row['token']); |
142
|
|
|
|
143
|
|
|
return $credential; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.