90 lines
3.3 KiB
PHP
90 lines
3.3 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Services\Translate\Completed\Project;
|
|
|
|
use App\Dto\Service\ProjectTranslationServiceHash\Hashes;
|
|
use App\Exceptions\Services\Translate\CompletedException;
|
|
use App\Jobs\Translate\ProcessProjectContent;
|
|
use App\Models\Project;
|
|
use App\Models\ProjectContent;
|
|
use App\Repositories\ProjectRepository;
|
|
use App\Services\ProjectTranslationService\NoTranslateAttributeHandler;
|
|
use App\Services\ProjectTranslationServiceHash\CompletionChecker;
|
|
use Illuminate\Support\Facades\DB;
|
|
use KorElf\TranslateLaravel\Contracts\TranslationCompletedListener;
|
|
|
|
final readonly class ProjectContentService implements TranslationCompletedListener
|
|
{
|
|
public function __construct(
|
|
private ProjectRepository $projectRepository,
|
|
private NoTranslateAttributeHandler $noTranslateAttributeHandler,
|
|
private CompletionChecker $completionChecker,
|
|
) { }
|
|
|
|
/**
|
|
* @throws CompletedException
|
|
*/
|
|
public function onTranslationCompleted(array $translatedText, array $data = []): void
|
|
{
|
|
if (
|
|
!isset($data['projectId'])
|
|
|| !isset($data['languageId'])
|
|
|| !isset($data['hashes'])
|
|
) {
|
|
throw new CompletedException('Required data is missing: projectId, languageId, or hashes.');
|
|
}
|
|
|
|
$project = $this->projectRepository->getProjectById((int) $data['projectId']);
|
|
if ($project === null) {
|
|
return;
|
|
}
|
|
|
|
$projectContent = DB::transaction(function () use ($data, $project, $translatedText) {
|
|
$values = [];
|
|
$hashes = $this->completionChecker->execute(
|
|
$this->getHashes($data['hashes']),
|
|
);
|
|
|
|
foreach ($translatedText as $translatedTextKey => $translatedTextValue) {
|
|
if ($hashes->isStatusWaiting($translatedTextKey) !== true) {
|
|
continue;
|
|
}
|
|
|
|
$translatedTextValue = $this->noTranslateAttributeHandler->handleRemoveAttribute($translatedTextValue);
|
|
$values[$translatedTextKey] = $translatedTextValue;
|
|
}
|
|
if (\count($values) === 0) {
|
|
return null;
|
|
}
|
|
|
|
$projectContent = $project->contents()->where('language_id', $data['languageId'])->first();
|
|
if ($projectContent !== null) {
|
|
$projectContent->update($values);
|
|
return $projectContent;
|
|
}
|
|
|
|
$values['language_id'] = $data['languageId'];
|
|
return $project->contents()->create($values);
|
|
});
|
|
if (\config('translation_service.enable', false) && $projectContent !== null) {
|
|
$this->translateContent($project, $projectContent, $data);
|
|
}
|
|
}
|
|
|
|
private function translateContent(Project $project, ProjectContent $projectContent, array $data): void
|
|
{
|
|
$translateExceptLanguages = $data['exceptLanguages'] ?? [];
|
|
$translateExceptLanguages[] = $data['languageId'];
|
|
ProcessProjectContent::dispatch($project->id, [$projectContent->id], $translateExceptLanguages);
|
|
}
|
|
|
|
private function getHashes(array $hashes): Hashes
|
|
{
|
|
$hashesDto = new Hashes();
|
|
foreach ($hashes as $field => $hash) {
|
|
$hashesDto->add((int) $hash['hashId'], $field, $hash['hash']);
|
|
}
|
|
return $hashesDto;
|
|
}
|
|
}
|