init
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\Html\TranslatableTextExtractor;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
|
||||
class TranslationLogger
|
||||
{
|
||||
private const TABLE_NAME = 'esun_localetranslation_log';
|
||||
private const MAX_LOG_FRAGMENT_BYTES = 30000;
|
||||
|
||||
public function __construct(
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly TranslatableTextExtractor $textExtractor
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $fields
|
||||
* @param array<string, string> $translations
|
||||
*/
|
||||
public function logSuccess(
|
||||
string $provider,
|
||||
string $entityType,
|
||||
int $entityId,
|
||||
int $sourceStoreId,
|
||||
int $targetStoreId,
|
||||
string $sourceLanguage,
|
||||
string $targetLanguage,
|
||||
array $fields,
|
||||
array $translations,
|
||||
string $status = 'success'
|
||||
): void {
|
||||
$rows = [];
|
||||
foreach ($fields as $fieldName => $field) {
|
||||
if (!is_array($field) || !array_key_exists('value', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fieldName = (string)$fieldName;
|
||||
if (!array_key_exists($fieldName, $translations)) {
|
||||
continue;
|
||||
}
|
||||
$isHtml = !empty($field['is_html']);
|
||||
$sourceText = $this->textForLog((string)$field['value'], $isHtml);
|
||||
$translatedText = $this->textForLog((string)$translations[$fieldName], $isHtml);
|
||||
$rows[] = $this->buildRow(
|
||||
$provider,
|
||||
$entityType,
|
||||
$entityId,
|
||||
$fieldName,
|
||||
$sourceStoreId,
|
||||
$targetStoreId,
|
||||
$sourceLanguage,
|
||||
$targetLanguage,
|
||||
$sourceText,
|
||||
$translatedText,
|
||||
$status,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
$this->insertRows($rows);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $fields */
|
||||
public function logFailure(
|
||||
string $provider,
|
||||
string $entityType,
|
||||
int $entityId,
|
||||
int $sourceStoreId,
|
||||
int $targetStoreId,
|
||||
string $sourceLanguage,
|
||||
string $targetLanguage,
|
||||
array $fields,
|
||||
string $errorMessage
|
||||
): void {
|
||||
$rows = [];
|
||||
foreach ($fields as $fieldName => $field) {
|
||||
if (!is_array($field) || !array_key_exists('value', $field)) {
|
||||
continue;
|
||||
}
|
||||
$sourceText = $this->textForLog((string)$field['value'], !empty($field['is_html']));
|
||||
$rows[] = $this->buildRow(
|
||||
$provider,
|
||||
$entityType,
|
||||
$entityId,
|
||||
(string)$fieldName,
|
||||
$sourceStoreId,
|
||||
$targetStoreId,
|
||||
$sourceLanguage,
|
||||
$targetLanguage,
|
||||
$sourceText,
|
||||
null,
|
||||
'failed',
|
||||
$errorMessage
|
||||
);
|
||||
}
|
||||
|
||||
if ($rows === []) {
|
||||
$rows[] = $this->buildRow(
|
||||
$provider,
|
||||
$entityType,
|
||||
$entityId,
|
||||
'',
|
||||
$sourceStoreId,
|
||||
$targetStoreId,
|
||||
$sourceLanguage,
|
||||
$targetLanguage,
|
||||
'',
|
||||
null,
|
||||
'failed',
|
||||
$errorMessage
|
||||
);
|
||||
}
|
||||
|
||||
$this->insertRows($rows);
|
||||
}
|
||||
|
||||
private function textForLog(string $value, bool $isHtml): string
|
||||
{
|
||||
if (!$isHtml || $value === '') {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$fragments = $this->textExtractor->extract($value, self::MAX_LOG_FRAGMENT_BYTES);
|
||||
|
||||
return trim(implode("\n", array_column($fragments, 'text')));
|
||||
}
|
||||
|
||||
/** @return array<string, int|string|null> */
|
||||
private function buildRow(
|
||||
string $provider,
|
||||
string $entityType,
|
||||
int $entityId,
|
||||
string $fieldName,
|
||||
int $sourceStoreId,
|
||||
int $targetStoreId,
|
||||
string $sourceLanguage,
|
||||
string $targetLanguage,
|
||||
string $sourceText,
|
||||
?string $translatedText,
|
||||
string $status,
|
||||
?string $errorMessage
|
||||
): array {
|
||||
return [
|
||||
'provider' => substr($provider, 0, 32),
|
||||
'entity_type' => $entityType !== '' ? substr($entityType, 0, 32) : null,
|
||||
'entity_id' => $entityId > 0 ? $entityId : null,
|
||||
'field_name' => $fieldName !== '' ? substr($fieldName, 0, 128) : null,
|
||||
'source_store_id' => max(0, $sourceStoreId),
|
||||
'target_store_id' => max(0, $targetStoreId),
|
||||
'source_language' => $sourceLanguage !== '' ? substr($sourceLanguage, 0, 16) : null,
|
||||
'target_language' => $targetLanguage !== '' ? substr($targetLanguage, 0, 16) : null,
|
||||
'source_text' => $sourceText,
|
||||
'translated_text' => $translatedText,
|
||||
'status' => substr($status, 0, 16),
|
||||
'error_message' => $errorMessage,
|
||||
'character_count' => mb_strlen($sourceText, 'UTF-8'),
|
||||
];
|
||||
}
|
||||
|
||||
/** @param array<int, array<string, int|string|null>> $rows */
|
||||
private function insertRows(array $rows): void
|
||||
{
|
||||
if ($rows === []) {
|
||||
return;
|
||||
}
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$connection->insertMultiple(
|
||||
$this->resourceConnection->getTableName(self::TABLE_NAME),
|
||||
$rows
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user