procyonweb /
laravel-translation-generator
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace ProcyonWeb\TranslationGenerator; |
||||
| 6 | |||||
| 7 | use GuzzleHttp\Client; |
||||
| 8 | use GuzzleHttp\RequestOptions; |
||||
| 9 | use Illuminate\Support\Facades\Config; |
||||
| 10 | use Symfony\Component\Console\Output\ConsoleOutput; |
||||
| 11 | |||||
| 12 | class PhraseClient |
||||
| 13 | { |
||||
| 14 | private const BASE_URL = 'https://api.phrase.com/v2/'; |
||||
| 15 | |||||
| 16 | /** @var Client */ |
||||
| 17 | private $client; |
||||
| 18 | |||||
| 19 | /** @var string|null */ |
||||
| 20 | private $projectId; |
||||
| 21 | |||||
| 22 | /** @var ConsoleOutput */ |
||||
| 23 | private $output; |
||||
| 24 | |||||
| 25 | /** @var string */ |
||||
| 26 | private $apiToken; |
||||
| 27 | |||||
| 28 | public function __construct(ConsoleOutput $output) |
||||
| 29 | { |
||||
| 30 | $this->output = $output; |
||||
| 31 | $apiToken = config('translation.phrase.apiToken', null); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 32 | $projectId = config('translation.phrase.projectId', null); |
||||
| 33 | |||||
| 34 | if ($apiToken && $projectId) { |
||||
| 35 | $this->client = new Client( |
||||
| 36 | [ |
||||
| 37 | 'base_uri' => self::BASE_URL |
||||
| 38 | ] |
||||
| 39 | ); |
||||
| 40 | $this->projectId = $projectId; |
||||
| 41 | $this->apiToken = $apiToken; |
||||
| 42 | } |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | public function isReady(): bool |
||||
| 46 | { |
||||
| 47 | return $this->client !== null; |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | public function uploadFile(string $data, string $filename, string $langIso): bool |
||||
|
0 ignored issues
–
show
The parameter
$data is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 51 | { |
||||
| 52 | try { |
||||
| 53 | $this->client->post( |
||||
| 54 | sprintf('projects/%s/uploads', $this->projectId), |
||||
| 55 | [ |
||||
| 56 | RequestOptions::AUTH => [$this->apiToken, ''], |
||||
| 57 | RequestOptions::MULTIPART => [ |
||||
| 58 | [ |
||||
| 59 | 'name' => 'locale_id', |
||||
| 60 | 'contents' => $langIso |
||||
| 61 | ], |
||||
| 62 | [ |
||||
| 63 | 'name' => 'update_translations', |
||||
| 64 | 'contents' => true |
||||
| 65 | ], |
||||
| 66 | [ |
||||
| 67 | 'name' => 'file_format', |
||||
| 68 | 'contents' => 'simple_json' |
||||
| 69 | ], |
||||
| 70 | [ |
||||
| 71 | 'name' => 'file', |
||||
| 72 | 'contents' => fopen($filename, 'r') |
||||
| 73 | ] |
||||
| 74 | ] |
||||
| 75 | ] |
||||
| 76 | ); |
||||
| 77 | $this->output->writeln(sprintf('[PHRASE] %s file uploaded.', $filename)); |
||||
| 78 | |||||
| 79 | return true; |
||||
| 80 | } catch (\Throwable $e) { |
||||
| 81 | $this->output->writeln( |
||||
| 82 | sprintf( |
||||
| 83 | '[PHRASE] Failed to upload file: %s. %s', |
||||
| 84 | $filename, |
||||
| 85 | $e->getMessage() |
||||
| 86 | ) |
||||
| 87 | ); |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | return false; |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | public function downloadFiles(): bool |
||||
| 94 | { |
||||
| 95 | try { |
||||
| 96 | $locales = config('translation.phrase.locales', []); |
||||
|
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 97 | |||||
| 98 | if (count($locales) === 0) { |
||||
| 99 | $this->output->writeln('[PHRASE] No locales configured.'); |
||||
| 100 | return false; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | $this->output->writeln(sprintf('[PHRASE] Downloading %d translations.', count($locales))); |
||||
| 104 | |||||
| 105 | foreach ($locales as $locale) { |
||||
| 106 | $this->downloadFile($locale); |
||||
| 107 | } |
||||
| 108 | |||||
| 109 | return true; |
||||
| 110 | } catch (\Throwable $e) { |
||||
| 111 | $this->output->writeln( |
||||
| 112 | sprintf( |
||||
| 113 | '[PHRASE] Failed to download files. %s', |
||||
| 114 | $e->getMessage() |
||||
| 115 | ) |
||||
| 116 | ); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | return false; |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | private function downloadFile(string $locale): void |
||||
| 123 | { |
||||
| 124 | $this->output->writeln(sprintf('[PHRASE] Downloading locale "%s".', $locale)); |
||||
| 125 | |||||
| 126 | $path = resource_path('lang/') . $locale . '.json'; |
||||
|
0 ignored issues
–
show
The function
resource_path was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 127 | $file = fopen($path, 'wb'); |
||||
| 128 | |||||
| 129 | $response = $this->client->get( |
||||
| 130 | sprintf('projects/%s/locales/%s/download', $this->projectId, $locale), |
||||
| 131 | array_merge_recursive( |
||||
| 132 | [ |
||||
| 133 | RequestOptions::AUTH => [$this->apiToken, ''], |
||||
| 134 | RequestOptions::SINK => $file, |
||||
| 135 | RequestOptions::QUERY => [ |
||||
| 136 | 'file_format' => 'simple_json', |
||||
| 137 | ] |
||||
| 138 | ], |
||||
| 139 | Config::get('translation.phrase.downloadOptions', []) |
||||
| 140 | ) |
||||
| 141 | ); |
||||
| 142 | |||||
| 143 | if ($response->getStatusCode() !== 200) { |
||||
| 144 | throw new \Exception($response->getReasonPhrase()); |
||||
| 145 | } |
||||
| 146 | } |
||||
| 147 | } |