Clearing the cache using tags did not work correctly. Had to abandon them.
112 lines
4.2 KiB
PHP
112 lines
4.2 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Services\Admin\Project;
|
|
|
|
use App\Dto\Service\Admin\Project\Translation\Translations;
|
|
use App\Dto\Service\Admin\Project\Translation\Translation;
|
|
use App\Dto\Service\Admin\Project\Translation\Update;
|
|
use App\Jobs\Translate\ProcessTranslationText;
|
|
use App\Models\ProjectTranslation;
|
|
use App\Models\User;
|
|
use App\Repositories\ProjectRepository;
|
|
use App\Repositories\ProjectTranslationRepository;
|
|
use App\ServiceResults\ServiceResultArray;
|
|
use App\ServiceResults\ServiceResultError;
|
|
use App\ServiceResults\ServiceResultSuccess;
|
|
use App\Services\ClearCacheCommandHandler;
|
|
use App\Services\ProjectTranslation\ModelSyncCommand;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class TranslationService extends Service
|
|
{
|
|
public function __construct(
|
|
private readonly ProjectRepository $projectRepository,
|
|
private readonly ProjectTranslationRepository $projectTranslationRepository,
|
|
private readonly ClearCacheCommandHandler $clearCacheCommandHandler,
|
|
private readonly ModelSyncCommand $translationModelSyncCommand,
|
|
) { }
|
|
|
|
public function languages(int $projectId, User $user): ServiceResultError | ServiceResultArray
|
|
{
|
|
$project = $this->projectRepository->getProjectById($projectId);
|
|
if (\is_null($project)) {
|
|
return $this->errNotFound(__('Not Found'));
|
|
}
|
|
|
|
if ($user->cannot('viewAny', ProjectTranslation::class)) {
|
|
return $this->errFobidden(__('Access is denied'));
|
|
}
|
|
|
|
return $this->result([
|
|
'project' => $project,
|
|
]);
|
|
}
|
|
|
|
public function edit(int $projectId, int $languageId, User $user): ServiceResultError | ServiceResultArray
|
|
{
|
|
$project = $this->projectRepository->getProjectById($projectId);
|
|
$language = $project?->languages()->firstWhere('id', $languageId);
|
|
|
|
if (\is_null($project) || \is_null($language)) {
|
|
return $this->errNotFound(__('Not Found'));
|
|
}
|
|
|
|
if ($user->cannot('viewAny', ProjectTranslation::class)) {
|
|
return $this->errFobidden(__('Access is denied'));
|
|
}
|
|
|
|
return $this->result([
|
|
'project' => $project,
|
|
'language' => $language,
|
|
'projectTranslations' => $this->projectTranslationRepository->getProjectTranslations($projectId, $languageId)->all()->pluck('text', 'code')->toArray(),
|
|
'translations' => Translations::getTranslationCodes(),
|
|
'serviceTranslationEnable' => config('translation_service.enable', false),
|
|
]);
|
|
}
|
|
|
|
public function update(int $projectId, int $languageId, Update $data, User $user): ServiceResultError | ServiceResultSuccess
|
|
{
|
|
$project = $this->projectRepository->getProjectById($projectId);
|
|
$language = $project?->languages()->firstWhere('id', $languageId);
|
|
|
|
if (\is_null($project) || \is_null($language)) {
|
|
return $this->errNotFound(__('Not Found'));
|
|
}
|
|
|
|
if ($user->cannot('update', ProjectTranslation::class)) {
|
|
return $this->errFobidden(__('Access is denied'));
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($data, $project, $language) {
|
|
$this->translationModelSyncCommand->execute($project, $language, $data->getTranslations());
|
|
});
|
|
$this->clearCacheCommandHandler->all();
|
|
if (\config('translation_service.enable', false)) {
|
|
$this->translateContent($projectId, $languageId, $data);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
return $this->errService(__('Server Error'));
|
|
}
|
|
|
|
return $this->ok(__('Translations successfully updated'));
|
|
}
|
|
|
|
private function translateContent(int $projectId, int $languageId, Update $data): void
|
|
{
|
|
if (! $data->isTranslateAutomatically()) {
|
|
return;
|
|
}
|
|
$translateExceptLanguages = [$languageId];
|
|
$translateTextCode = [];
|
|
foreach ($data->getTranslations()->getTranslations() as $translation) {
|
|
/** @var Translation $translation */
|
|
$translateTextCode[] = $translation->getCode();
|
|
}
|
|
|
|
ProcessTranslationText::dispatch($projectId, $languageId, $translateTextCode, $translateExceptLanguages);
|
|
}
|
|
}
|