forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildMetaStore.php
More file actions
169 lines (142 loc) · 4.42 KB
/
Copy pathBuildMetaStore.php
File metadata and controls
169 lines (142 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace PHPCensor\Store;
use PHPCensor\Store;
use PHPCensor\Database;
use PHPCensor\Model\BuildMeta;
use PHPCensor\Exception\HttpException;
class BuildMetaStore extends Store
{
/**
* @var string
*/
protected $tableName = 'build_meta';
/**
* @var string
*/
protected $modelName = '\PHPCensor\Model\BuildMeta';
/**
* @var string
*/
protected $primaryKey = 'id';
/**
* Get a BuildMeta by primary key (Id)
*
* @param integer $key
* @param string $useConnection
*
* @return null|BuildMeta
*/
public function getByPrimaryKey($key, $useConnection = 'read')
{
return $this->getById($key, $useConnection);
}
/**
* Get a single BuildMeta by Id.
*
* @param integer $id
* @param string $useConnection
*
* @return null|BuildMeta
*
* @throws HttpException
*/
public function getById($id, $useConnection = 'read')
{
if (is_null($id)) {
throw new HttpException('id passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{id}} = :id LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
$stmt->bindValue(':id', $id);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new BuildMeta($data);
}
}
return null;
}
/**
* @param integer $buildId
* @param string $key
*
* @return null|BuildMeta
*
* @throws HttpException
*/
public function getByKey($buildId, $key)
{
if (is_null($buildId)) {
throw new HttpException('buildId passed to ' . __FUNCTION__ . ' cannot be null.');
}
if (!$key) {
throw new HttpException('key passed to ' . __FUNCTION__ . ' cannot be empty.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{build_id}} = :build_id AND {{meta_key}} = :meta_key LIMIT 1';
$stmt = Database::getConnection()->prepareCommon($query);
$stmt->bindValue(':build_id', $buildId);
$stmt->bindValue(':meta_key', $key);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new BuildMeta($data);
}
}
return null;
}
/**
* Get multiple BuildMeta by BuildId.
*
* @param integer $buildId
* @param integer $limit
* @param string $useConnection
*
* @return array
*
* @throws HttpException
*/
public function getByBuildId($buildId, $limit = 1000, $useConnection = 'read')
{
if (is_null($buildId)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{build_id}} = :build_id LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
$stmt->bindValue(':build_id', $buildId);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new BuildMeta($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
/**
* Only used by an upgrade migration to move errors from build_meta to build_error
*
* @param integer $limit
*
* @return array
*/
public function getErrorsForUpgrade($limit)
{
$query = 'SELECT * FROM {{' . $this->tableName . '}}
WHERE {{meta_key}} IN (\'phpmd-data\', \'phpcs-data\', \'phpdoccheck-data\', \'technical_debt-data\')
ORDER BY {{id}} ASC LIMIT :limit';
$stmt = Database::getConnection('read')->prepareCommon($query);
$stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new BuildMeta($item);
};
$rtn = array_map($map, $res);
return $rtn;
} else {
return [];
}
}
}