@@ -39,19 +39,15 @@ final class CrowdinProvider implements ProviderInterface
39
39
private $ xliffFileDumper ;
40
40
private $ defaultLocale ;
41
41
private $ endpoint ;
42
- private $ projectId ;
43
- private $ filesDownloader ;
44
42
45
- public function __construct (HttpClientInterface $ client , LoaderInterface $ loader , LoggerInterface $ logger , XliffFileDumper $ xliffFileDumper , string $ defaultLocale , string $ endpoint, int $ projectId , HttpClientInterface $ filesDownloader )
43
+ public function __construct (HttpClientInterface $ client , LoaderInterface $ loader , LoggerInterface $ logger , XliffFileDumper $ xliffFileDumper , string $ defaultLocale , string $ endpoint )
46
44
{
47
45
$ this ->client = $ client ;
48
46
$ this ->loader = $ loader ;
49
47
$ this ->logger = $ logger ;
50
48
$ this ->xliffFileDumper = $ xliffFileDumper ;
51
49
$ this ->defaultLocale = $ defaultLocale ;
52
50
$ this ->endpoint = $ endpoint ;
53
- $ this ->projectId = $ projectId ;
54
- $ this ->filesDownloader = $ filesDownloader ;
55
51
}
56
52
57
53
public function __toString (): string
@@ -102,7 +98,7 @@ public function write(TranslatorBagInterface $translatorBag): void
102
98
103
99
foreach ($ responses as $ response ) {
104
100
if (200 !== $ response ->getStatusCode ()) {
105
- $ this ->logger ->error (sprintf ('Unable to upload translations to Crowdin. Message : "%s". ' , $ response ->getContent (false )));
101
+ $ this ->logger ->error (sprintf ('Unable to upload translations to Crowdin: "%s". ' , $ response ->getContent (false )));
106
102
}
107
103
}
108
104
}
@@ -128,39 +124,37 @@ public function read(array $domains, array $locales): TranslatorBag
128
124
$ response = $ this ->downloadSourceFile ($ fileId );
129
125
}
130
126
131
- $ responses [] = [
132
- 'response ' => $ response ,
133
- 'locale ' => $ locale ,
134
- 'domain ' => $ domain ,
135
- ];
127
+ $ responses [] = [$ response , $ locale , $ domain ];
136
128
}
137
129
}
138
130
139
- foreach ($ responses as $ responseData ) {
140
- /** @var ResponseInterface $response */
141
- $ response = $ responseData ['response ' ];
142
-
131
+ /* @var ResponseInterface $response */
132
+ $ downloads = [];
133
+ foreach ($ responses as [$ response , $ locale , $ domain ]) {
143
134
if (204 === $ response ->getStatusCode ()) {
144
- $ this ->logger ->error (sprintf ('No content in exported file. Message : "%s". ' , $ response ->getContent (false )));
135
+ $ this ->logger ->error (sprintf ('No content in exported file: "%s". ' , $ response ->getContent (false )));
145
136
146
137
continue ;
147
138
}
148
139
149
140
if (200 !== $ response ->getStatusCode ()) {
150
- $ this ->logger ->error (sprintf ('Unable to export file. Message : "%s". ' , $ response ->getContent (false )));
141
+ $ this ->logger ->error (sprintf ('Unable to export file: "%s". ' , $ response ->getContent (false )));
151
142
152
143
continue ;
153
144
}
154
145
155
- $ exportResponse = $ this ->filesDownloader ->request ('GET ' , $ response ->toArray ()['data ' ]['url ' ]);
146
+ $ response = $ this ->client ->request ('GET ' , $ response ->toArray ()['data ' ]['url ' ]);
147
+ $ downloads [] = [$ response , $ locale , $ domain ];
148
+ }
156
149
157
- if (200 !== $ exportResponse ->getStatusCode ()) {
158
- $ this ->logger ->error (sprintf ('Unable to download file content. Message: "%s". ' , $ response ->getContent (false )));
150
+ foreach ($ downloads as [$ response , $ locale , $ domain ]) {
151
+ if (200 !== $ response ->getStatusCode ()) {
152
+ $ this ->logger ->error (sprintf ('Unable to download file content: "%s". ' , $ response ->getContent (false )));
159
153
160
154
continue ;
161
155
}
162
156
163
- $ translatorBag ->addCatalogue ($ this ->loader ->load ($ exportResponse ->getContent (), $ responseData [ ' locale ' ] , $ responseData [ ' domain ' ] ));
157
+ $ translatorBag ->addCatalogue ($ this ->loader ->load ($ response ->getContent (), $ locale , $ domain ));
164
158
}
165
159
166
160
return $ translatorBag ;
@@ -201,7 +195,7 @@ public function delete(TranslatorBagInterface $translatorBag): void
201
195
}
202
196
203
197
if (204 !== $ response ->getStatusCode ()) {
204
- $ this ->logger ->warning (sprintf ('Unable to delete string in project %d. Message : "%s". ' , $ this -> projectId , $ response ->getContent (false )));
198
+ $ this ->logger ->warning (sprintf ('Unable to delete string: "%s". ' , $ response ->getContent (false )));
205
199
}
206
200
}
207
201
}
@@ -239,15 +233,15 @@ private function addFile(string $domain, string $content): ?array
239
233
* @see https://support.crowdin.com/api/v2/#operation/api.projects.files.getMany (Crowdin API)
240
234
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.files.getMany (Crowdin Enterprise API)
241
235
*/
242
- $ response = $ this ->client ->request ('POST ' , sprintf ( ' projects/%s/ files ', $ this -> projectId ) , [
236
+ $ response = $ this ->client ->request ('POST ' , ' files ' , [
243
237
'json ' => [
244
238
'storageId ' => $ storageId ,
245
239
'name ' => sprintf ('%s.%s ' , $ domain , 'xlf ' ),
246
240
],
247
241
]);
248
242
249
243
if (201 !== $ response ->getStatusCode ()) {
250
- $ this ->logger ->error (sprintf ('Unable to create a File in Crowdin for domain "%s". Message : "%s". ' , $ domain , $ response ->getContent (false )));
244
+ $ this ->logger ->error (sprintf ('Unable to create a File in Crowdin for domain "%s": "%s". ' , $ domain , $ response ->getContent (false )));
251
245
252
246
return null ;
253
247
}
@@ -263,14 +257,14 @@ private function updateFile(int $fileId, string $domain, string $content): ?arra
263
257
* @see https://support.crowdin.com/api/v2/#operation/api.projects.files.put (Crowdin API)
264
258
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.files.put (Crowdin Enterprise API)
265
259
*/
266
- $ response = $ this ->client ->request ('PUT ' , sprintf ( ' projects/%s/ files/%d ' , $ this -> projectId , $ fileId) , [
260
+ $ response = $ this ->client ->request ('PUT ' , ' files/ ' . $ fileId , [
267
261
'json ' => [
268
262
'storageId ' => $ storageId ,
269
263
],
270
264
]);
271
265
272
266
if (200 !== $ response ->getStatusCode ()) {
273
- $ this ->logger ->error (sprintf ('Unable to update file in Crowdin for file ID "%d" and domain "%s". Message : "%s". ' , $ fileId , $ domain , $ response ->getContent (false )));
267
+ $ this ->logger ->error (sprintf ('Unable to update file in Crowdin for file ID "%d" and domain "%s": "%s". ' , $ fileId , $ domain , $ response ->getContent (false )));
274
268
275
269
return null ;
276
270
}
@@ -286,7 +280,7 @@ private function uploadTranslations(int $fileId, string $domain, string $content
286
280
* @see https://support.crowdin.com/api/v2/#operation/api.projects.translations.postOnLanguage (Crowdin API)
287
281
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.translations.postOnLanguage (Crowdin Enterprise API)
288
282
*/
289
- return $ this ->client ->request ('POST ' , sprintf ( ' projects/%s/ translations/%s ' , $ this -> projectId , $ locale) , [
283
+ return $ this ->client ->request ('POST ' , ' translations/ ' . $ locale , [
290
284
'json ' => [
291
285
'storageId ' => $ storageId ,
292
286
'fileId ' => $ fileId ,
@@ -300,7 +294,7 @@ private function exportProjectTranslations(string $languageId, int $fileId): Res
300
294
* @see https://support.crowdin.com/api/v2/#operation/api.projects.translations.exports.post (Crowdin API)
301
295
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.translations.exports.post (Crowdin Enterprise API)
302
296
*/
303
- return $ this ->client ->request ('POST ' , sprintf ( ' projects/%d/ translations/exports ', $ this -> projectId ) , [
297
+ return $ this ->client ->request ('POST ' , ' translations/exports ' , [
304
298
'json ' => [
305
299
'targetLanguageId ' => $ languageId ,
306
300
'fileIds ' => [$ fileId ],
@@ -314,7 +308,7 @@ private function downloadSourceFile(int $fileId): ResponseInterface
314
308
* @see https://support.crowdin.com/api/v2/#operation/api.projects.files.download.get (Crowdin API)
315
309
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.files.download.get (Crowdin Enterprise API)
316
310
*/
317
- return $ this ->client ->request ('GET ' , sprintf ('projects/%d/ files/%d/download ' , $ this -> projectId , $ fileId ));
311
+ return $ this ->client ->request ('GET ' , sprintf ('files/%d/download ' , $ fileId ));
318
312
}
319
313
320
314
private function listStrings (int $ fileId , int $ limit , int $ offset ): array
@@ -323,7 +317,7 @@ private function listStrings(int $fileId, int $limit, int $offset): array
323
317
* @see https://support.crowdin.com/api/v2/#operation/api.projects.strings.getMany (Crowdin API)
324
318
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.strings.getMany (Crowdin Enterprise API)
325
319
*/
326
- $ response = $ this ->client ->request ('GET ' , sprintf ( ' projects/%d/ strings ', $ this -> projectId ) , [
320
+ $ response = $ this ->client ->request ('GET ' , ' strings ' , [
327
321
'query ' => [
328
322
'fileId ' => $ fileId ,
329
323
'limit ' => $ limit ,
@@ -332,7 +326,7 @@ private function listStrings(int $fileId, int $limit, int $offset): array
332
326
]);
333
327
334
328
if (200 !== $ response ->getStatusCode ()) {
335
- $ this ->logger ->error (sprintf ('Unable to list strings for file %d in project %d. Message : "%s". ' , $ fileId, $ this -> projectId , $ response ->getContent ()));
329
+ $ this ->logger ->error (sprintf ('Unable to list strings for file %d: "%s". ' , $ fileId , $ response ->getContent ()));
336
330
337
331
return [];
338
332
}
@@ -346,7 +340,7 @@ private function deleteString(int $stringId): ResponseInterface
346
340
* @see https://support.crowdin.com/api/v2/#operation/api.projects.strings.delete (Crowdin API)
347
341
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.strings.delete (Crowdin Enterprise API)
348
342
*/
349
- return $ this ->client ->request ('DELETE ' , sprintf ( ' projects/%d/ strings/%d ' , $ this -> projectId , $ stringId) );
343
+ return $ this ->client ->request ('DELETE ' , ' strings/ ' . $ stringId );
350
344
}
351
345
352
346
private function addStorage (string $ domain , string $ content ): int
@@ -355,7 +349,7 @@ private function addStorage(string $domain, string $content): int
355
349
* @see https://support.crowdin.com/api/v2/#operation/api.storages.post (Crowdin API)
356
350
* @see https://support.crowdin.com/enterprise/api/#operation/api.storages.post (Crowdin Enterprise API)
357
351
*/
358
- $ response = $ this ->client ->request ('POST ' , 'storages ' , [
352
+ $ response = $ this ->client ->request ('POST ' , '../../ storages ' , [
359
353
'headers ' => [
360
354
'Crowdin-API-FileName ' => urlencode (sprintf ('%s.%s ' , $ domain , 'xlf ' )),
361
355
'Content-Type ' => 'application/octet-stream ' ,
@@ -378,7 +372,7 @@ private function getFileList(): array
378
372
* @see https://support.crowdin.com/api/v2/#operation/api.projects.files.getMany (Crowdin API)
379
373
* @see https://support.crowdin.com/enterprise/api/#operation/api.projects.files.getMany (Crowdin Enterprise API)
380
374
*/
381
- $ response = $ this ->client ->request ('GET ' , sprintf ( ' projects/%d/ files ', $ this -> projectId ) );
375
+ $ response = $ this ->client ->request ('GET ' , ' files ' );
382
376
383
377
if (200 !== $ response ->getStatusCode ()) {
384
378
throw new ProviderException ('Unable to list Crowdin files. ' , $ response );
0 commit comments