Add DTOs, exceptions, and jobs for translation service.
Introduced new Data Transfer Objects (DTOs), exceptions, and jobs to enhance the translation service functionality. Updated namespaces for consistency and added rate limiting to the translation provider. Expanded the README with detailed usage instructions.
This commit is contained in:
86
src/Jobs/ProcessTranslate.php
Normal file
86
src/Jobs/ProcessTranslate.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace KorElf\TranslateLaravel\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\Middleware\RateLimited;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use KorElf\TranslateLaravel\Contracts\ProcessTranslateContract;
|
||||
use KorElf\TranslateLaravel\DTO\ProcessTranslateDto;
|
||||
use KorElf\TranslateLaravel\DTO\ProcessTranslateLimit;
|
||||
use KorElf\TranslateLaravel\Facades\Translate;
|
||||
|
||||
final class ProcessTranslate implements ShouldQueue, ShouldBeEncrypted, ProcessTranslateContract
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly ProcessTranslateDto $param
|
||||
) { }
|
||||
|
||||
/**
|
||||
* Get the middleware the job should pass through.
|
||||
*
|
||||
* @return array<int, object>
|
||||
*/
|
||||
public function middleware(): array
|
||||
{
|
||||
return [
|
||||
new RateLimited('kor-elf-translate'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getLimited(): ProcessTranslateLimit
|
||||
{
|
||||
$driver = $this->param->getDriver();
|
||||
if ($driver === null) {
|
||||
$driver = Translate::getDefaultDriver();
|
||||
}
|
||||
$limit = Translate::getLimit($driver);
|
||||
|
||||
return new ProcessTranslateLimit(
|
||||
maxRequest: $limit['max_request'] ?? 1000000000,
|
||||
rateSeconds: $limit['rate_seconds'] ?? 1,
|
||||
driver: $driver,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$param = $this->param;
|
||||
$groupName = $param->getGroupName();
|
||||
|
||||
$translated = Cache::get($groupName, []);
|
||||
if (!isset($translated[$param->getKey()])) {
|
||||
$translated[$param->getKey()] = [];
|
||||
}
|
||||
|
||||
$translate = Translate::service($param->getDriver());
|
||||
$function = $param->getTextType()->functionName();
|
||||
$key = $param->getKey();
|
||||
$part = $param->getPart();
|
||||
|
||||
$translated[$key][$part] = $param->getText();
|
||||
if (\trim($param->getText()) !== '') {
|
||||
|
||||
$translated[$key][$part] = $translate->{$function}(
|
||||
$param->getText(),
|
||||
$param->getTargetLanguageCode(),
|
||||
$param->getSourceLanguageCode()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
Cache::put($groupName, $translated, 86400);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user