$fields * @param array $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 $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 */ 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> $rows */ private function insertRows(array $rows): void { if ($rows === []) { return; } $connection = $this->resourceConnection->getConnection(); $connection->insertMultiple( $this->resourceConnection->getTableName(self::TABLE_NAME), $rows ); } }