Repository navigation
Expand file tree
/
Copy pathCommitTranslationController.php
More file actions
40 lines (33 loc) · 1.7 KB
/
Copy pathCommitTranslationController.php
File metadata and controls
40 lines (33 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api;
use App\Actions\CreateCommitTranslation;
use App\Exceptions\CommitProcessingFailed;
use App\Http\Requests\CreateApiCommitTranslationRequest;
use App\Http\Resources\ShownGuildMentions;
use App\Models\Commit;
use App\Services\CommitTranslations;
use Illuminate\Http\JsonResponse;
use Throwable;
final readonly class CommitTranslationController
{
/**
* The commit in the language asked for, as Translate on the web shows it. A failed translation is a 503 the app
* can retry, with the same message the web shows.
*/
public function store(CreateApiCommitTranslationRequest $request, Commit $commit, CreateCommitTranslation $action, CommitTranslations $translations): JsonResponse
{
try {
$action->handle($commit, $request->string('language')->value());
} catch (Throwable $throwable) {
report($throwable instanceof CommitProcessingFailed ? $throwable : new CommitProcessingFailed('Translation', $commit->id, $throwable));
$message = $throwable instanceof CommitProcessingFailed && $throwable->reason === 'unsupported_source_language'
? 'The translation service returned an unsupported source language. Try again.'
: 'Could not translate this commit. Try again.';
return response()->json(['message' => $message], 503)
->header('Cache-Control', 'no-store, private');
}
return response()->json(ShownGuildMentions::of(['translation' => $translations->display($commit, $request->string('language')->value(), includePending: true)], $request->user()))
->header('Cache-Control', 'no-store, private');
}
}