php-censor/src/PHPCensor/Store/BuildErrorWriter.php

136 lines
3.6 KiB
PHP
Raw Normal View History

2017-05-08 07:32:01 +02:00
<?php
namespace PHPCensor\Store;
use b8\Config;
2017-05-08 07:32:01 +02:00
use b8\Database;
/**
* Class BuildErrorWriter
*/
class BuildErrorWriter
{
2017-11-05 15:48:36 +01:00
/**
* @var integer
*/
2017-10-14 20:29:29 +02:00
protected $buildId;
2017-05-08 07:32:01 +02:00
2017-11-05 15:48:36 +01:00
/**
* @var array
*/
2017-05-08 07:32:01 +02:00
protected $errors = [];
/**
2017-11-05 15:48:36 +01:00
* @var integer
*
* @see https://stackoverflow.com/questions/40361164/pdoexception-sqlstatehy000-general-error-7-number-of-parameters-must-be-bet
*/
2017-10-14 20:29:29 +02:00
protected $bufferSize;
2017-05-08 07:32:01 +02:00
/**
* BuildErrorWriter constructor.
*
2017-10-14 20:29:29 +02:00
* @param int $buildId
2017-05-08 07:32:01 +02:00
*/
2017-10-14 20:29:29 +02:00
public function __construct($buildId)
2017-05-08 07:32:01 +02:00
{
2017-10-14 20:29:29 +02:00
$this->bufferSize = (integer)Config::getInstance()->get('php-censor.build.writer_buffer_size', 500);
$this->buildId = $buildId;
2017-05-08 07:32:01 +02:00
}
/**
* Destructor
*/
public function __destruct()
{
$this->flush();
}
/**
* Write error
*
* @param string $plugin
* @param string $message
2017-11-05 15:48:36 +01:00
* @param integer $severity
2017-05-08 07:32:01 +02:00
* @param string $file
2017-11-05 15:48:36 +01:00
* @param integer $lineStart
* @param integer $lineEnd
2017-10-14 20:29:29 +02:00
* @param \DateTime $createdDate
2017-05-08 07:32:01 +02:00
*/
public function write(
$plugin,
$message,
$severity,
$file = null,
2017-10-14 20:29:29 +02:00
$lineStart = null,
$lineEnd = null,
$createdDate = null
) {
2017-10-14 20:29:29 +02:00
if (is_null($createdDate)) {
$createdDate = new \DateTime();
2017-05-08 07:32:01 +02:00
}
$this->errors[] = [
'plugin' => (string)$plugin,
'message' => (string)$message,
'severity' => (int)$severity,
'file' => !is_null($file) ? (string)$file : null,
'line_start' => !is_null($lineStart) ? (int)$lineStart : null,
'line_end' => !is_null($lineEnd) ? (int)$lineEnd : null,
'create_date' => $createdDate->format('Y-m-d H:i:s'),
];
2017-10-14 20:29:29 +02:00
if (count($this->errors) >= $this->bufferSize) {
2017-05-08 07:32:01 +02:00
$this->flush();
}
}
/**
* Flush buffer
*/
public function flush()
{
if (empty($this->errors)) {
return;
}
2017-10-14 20:29:29 +02:00
$insertValuesPlaceholders = [];
$insertValuesData = [];
2017-05-08 07:32:01 +02:00
foreach ($this->errors as $i => $error) {
2017-10-14 20:29:29 +02:00
$insertValuesPlaceholders[] = '(
2017-05-08 07:32:01 +02:00
:build_id' . $i . ',
:plugin' . $i . ',
:file' . $i . ',
:line_start' . $i . ',
:line_end' . $i . ',
:severity' . $i . ',
:message' . $i . ',
:create_date' . $i . '
2017-05-08 07:32:01 +02:00
)';
$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'];
2017-05-08 07:32:01 +02:00
}
$query = '
INSERT INTO {{build_error}} (
{{build_id}},
{{plugin}},
{{file}},
{{line_start}},
{{line_end}},
{{severity}},
{{message}},
{{create_date}}
2017-05-08 07:32:01 +02:00
)
2017-10-14 20:29:29 +02:00
VALUES ' . join(', ', $insertValuesPlaceholders) . '
2017-05-08 07:32:01 +02:00
';
$stmt = Database::getConnection('write')->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->execute($insertValuesData);
2017-05-08 07:32:01 +02:00
$this->errors = [];
}
}