Files
online.tbbank.gov.tm-larave…/app/Repos/System/Locale/LocaleManagerRepo.php
2024-09-01 18:40:53 +05:00

102 lines
2.2 KiB
PHP

<?php
namespace App\Repos\System\Locale;
use Exception;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Symfony\Component\Filesystem\Filesystem;
class LocaleManagerRepo
{
/**
* Locale app path
*/
protected string $localeAppPath;
/**
* Locale app url
*/
protected string $localeAppUrl;
/**
* Locale app api token
*/
protected string $localeAppApiToken;
/**
* Files system
*/
protected Filesystem $fileSystem;
/**
* Locale manager app
*/
public function __construct()
{
$this->localeAppPath = config('app.locale_app.path');
$this->localeAppUrl = config('app.locale_app.url');
$this->localeAppApiToken = config('app.locale_app.api_token');
$this->fileSystem = new Filesystem;
}
/**
* Magic way of new
*/
public static function make(): self
{
return new self;
}
/**
* Run locale manager
*/
public function handle(): void
{
$this->exportTranslations()
->syncTranslationsWithLocaleApp();
}
/**
* Locale app translations directory
*/
public function localeAppTranslationsDirectory(): string
{
return $this->localeAppPath.DIRECTORY_SEPARATOR.'lang';
}
/**
* Export translations to translate app
*/
public function exportTranslations(): self
{
$this->fileSystem->mirror(lang_path(), $this->localeAppTranslationsDirectory());
return $this;
}
/**
* Sync translations with locale app
*/
public function syncTranslationsWithLocaleApp(): self
{
$response = Http::acceptJson()->withHeaders([
'Api-Token' => $this->localeAppApiToken,
])
->retry(
times: 3,
sleepMilliseconds: 50,
throw: false,
when: function (Exception $exception, PendingRequest $request) {
return true;
})
->post(
url: $this->localeAppUrl.'/api/import-translations',
data: []
);
return $this;
}
}