-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathBuildErrorWriter.php
More file actions
144 lines (124 loc) · 4.33 KB
/
BuildErrorWriter.php
File metadata and controls
144 lines (124 loc) · 4.33 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
<?php
declare(strict_types=1);
namespace PHPCensor\Store;
use DateTime;
use PHPCensor\Common\Application\ConfigurationInterface;
use PHPCensor\DatabaseManager;
use PHPCensor\Model\BuildError;
use PHPCensor\StoreRegistry;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <[email protected]>
* @author Dmitry Khomutov <[email protected]>
*/
class BuildErrorWriter
{
private int $buildId;
private int $projectId;
private array $errors = [];
private DatabaseManager $databaseManager;
private StoreRegistry $storeRegistry;
/**
* @see https://stackoverflow.com/questions/40361164/pdoexception-sqlstatehy000-general-error-7-number-of-parameters-must-be-bet
*/
private int $bufferSize;
public function __construct(
ConfigurationInterface $configuration,
DatabaseManager $databaseManager,
StoreRegistry $storeRegistry,
int $projectId,
int $buildId
) {
$this->bufferSize = (int)$configuration->get('php-censor.build.writer_buffer_size', 500);
$this->projectId = $projectId;
$this->buildId = $buildId;
$this->databaseManager = $databaseManager;
$this->storeRegistry = $storeRegistry;
}
public function __destruct()
{
$this->flush();
}
public function write(
string $plugin,
string $message,
int $severity,
?string $file = null,
?int $lineStart = null,
?int $lineEnd = null,
?DateTime $createdDate = null
): void {
if (\is_null($createdDate)) {
$createdDate = new DateTime();
}
/** @var BuildErrorStore $errorStore */
$errorStore = $this->storeRegistry->get('BuildError');
$hash = BuildError::generateHash($plugin, $file, $lineStart, $lineEnd, $severity, $message);
$this->errors[] = [
'plugin' => $plugin,
'message' => $message,
'severity' => $severity,
'file' => $file,
'line_start' => $lineStart,
'line_end' => $lineEnd,
'create_date' => $createdDate->format('Y-m-d H:i:s'),
'hash' => $hash,
'is_new' => $errorStore->getIsNewError($this->projectId, $hash) ? 1 : 0,
];
if (\count($this->errors) >= $this->bufferSize) {
$this->flush();
}
}
public function flush(): void
{
if (empty($this->errors)) {
return;
}
$insertValuesPlaceholders = [];
$insertValuesData = [];
foreach ($this->errors as $i => $error) {
$insertValuesPlaceholders[] = '(
:build_id' . $i . ',
:plugin' . $i . ',
:file' . $i . ',
:line_start' . $i . ',
:line_end' . $i . ',
:severity' . $i . ',
:message' . $i . ',
:create_date' . $i . ',
:hash' . $i . ',
:is_new' . $i . '
)';
$insertValuesData['build_id' . $i] = $this->buildId;
$insertValuesData['plugin' . $i] = $error['plugin'];
$insertValuesData['file' . $i] = $error['file'];
$insertValuesData['line_start' . $i] = $error['line_start'];
$insertValuesData['line_end' . $i] = $error['line_end'];
$insertValuesData['severity' . $i] = $error['severity'];
$insertValuesData['message' . $i] = $error['message'];
$insertValuesData['create_date' . $i] = $error['create_date'];
$insertValuesData['hash' . $i] = $error['hash'];
$insertValuesData['is_new' . $i] = $error['is_new'];
}
$query = '
INSERT INTO {{build_errors}} (
{{build_id}},
{{plugin}},
{{file}},
{{line_start}},
{{line_end}},
{{severity}},
{{message}},
{{create_date}},
{{hash}},
{{is_new}}
)
VALUES ' . \join(', ', $insertValuesPlaceholders) . '
';
$stmt = $this->databaseManager->getConnection('write')->prepare($query);
$stmt->execute($insertValuesData);
$this->errors = [];
}
}