11
11
12
12
namespace Symfony \Component \Translation \Bridge \Loco ;
13
13
14
+ use Psr \Log \LoggerInterface ;
14
15
use Symfony \Component \HttpFoundation \Response ;
15
16
use Symfony \Component \Translation \Exception \TransportException ;
16
17
use Symfony \Component \Translation \Loader \LoaderInterface ;
@@ -33,14 +34,23 @@ class LocoRemote extends AbstractRemote
33
34
{
34
35
protected const HOST = 'localise.biz ' ;
35
36
37
+ /** @var string */
36
38
private $ apiKey ;
39
+
40
+ /** @var LoaderInterface|null */
37
41
private $ loader ;
42
+
43
+ /** @var LoggerInterface|null */
44
+ private $ logger ;
45
+
46
+ /** @var string|null */
38
47
private $ defaultLocale ;
39
48
40
- public function __construct (string $ apiKey , HttpClientInterface $ client = null , LoaderInterface $ loader = null , string $ defaultLocale = null )
49
+ public function __construct (string $ apiKey , HttpClientInterface $ client = null , LoaderInterface $ loader = null , LoggerInterface $ logger = null , string $ defaultLocale = null )
41
50
{
42
51
$ this ->apiKey = $ apiKey ;
43
52
$ this ->loader = $ loader ;
53
+ $ this ->logger = $ logger ;
44
54
$ this ->defaultLocale = $ defaultLocale ;
45
55
46
56
parent ::__construct ($ client );
@@ -84,9 +94,7 @@ public function read(array $domains, array $locales): TranslatorBag
84
94
85
95
foreach ($ locales as $ locale ) {
86
96
$ response = $ this ->client ->request ('GET ' , sprintf ('https://%s/api/export/locale/%s.xlf?filter=%s ' , $ this ->getEndpoint (), $ locale , $ filter ), [
87
- 'headers ' => [
88
- 'Authorization ' => 'Loco ' .$ this ->apiKey ,
89
- ],
97
+ 'headers ' => $ this ->getDefaultHeaders (),
90
98
]);
91
99
92
100
$ responseContent = $ response ->getContent (false );
@@ -110,8 +118,8 @@ public function delete(TranslatorBag $translations): void
110
118
{
111
119
$ deletedIds = [];
112
120
113
- foreach ($ translations ->all () as $ locale => $ messages ) {
114
- foreach ($ messages as $ domain => $ messages ) {
121
+ foreach ($ translations ->all () as $ locale => $ domainMessages ) {
122
+ foreach ($ domainMessages as $ domain => $ messages ) {
115
123
foreach ($ messages as $ id => $ message ) {
116
124
if (\in_array ($ id , $ deletedIds )) {
117
125
continue ;
@@ -125,12 +133,17 @@ public function delete(TranslatorBag $translations): void
125
133
}
126
134
}
127
135
136
+ protected function getDefaultHeaders (): array
137
+ {
138
+ return [
139
+ 'Authorization ' => 'Loco ' .$ this ->apiKey ,
140
+ ];
141
+ }
142
+
128
143
private function createAsset (string $ id ): void
129
144
{
130
145
$ response = $ this ->client ->request ('POST ' , sprintf ('https://%s/api/assets ' , $ this ->getEndpoint ()), [
131
- 'headers ' => [
132
- 'Authorization ' => 'Loco ' .$ this ->apiKey ,
133
- ],
146
+ 'headers ' => $ this ->getDefaultHeaders (),
134
147
'body ' => [
135
148
'name ' => $ id ,
136
149
'id ' => $ id ,
@@ -140,7 +153,9 @@ private function createAsset(string $id): void
140
153
]);
141
154
142
155
if (Response::HTTP_CONFLICT === $ response ->getStatusCode ()) {
143
- // Translation key already exists in Loco, do nothing
156
+ $ this ->logger ->warning (sprintf ('Translation key (%s) already exists in Loco. ' , $ id ), [
157
+ 'id ' => $ id ,
158
+ ]);
144
159
} elseif (Response::HTTP_CREATED !== $ response ->getStatusCode ()) {
145
160
throw new TransportException (sprintf ('Unable to add new translation key (%s) to Loco: (status code: "%s") "%s". ' , $ id , $ response ->getStatusCode (), $ response ->getContent (false )), $ response );
146
161
}
@@ -149,9 +164,7 @@ private function createAsset(string $id): void
149
164
private function translateAsset (string $ id , string $ message , string $ locale ): void
150
165
{
151
166
$ response = $ this ->client ->request ('POST ' , sprintf ('https://%s/api/translations/%s/%s ' , $ this ->getEndpoint (), $ id , $ locale ), [
152
- 'headers ' => [
153
- 'Authorization ' => 'Loco ' .$ this ->apiKey ,
154
- ],
167
+ 'headers ' => $ this ->getDefaultHeaders (),
155
168
'body ' => $ message ,
156
169
]);
157
170
@@ -169,9 +182,7 @@ private function tagsAssets(array $ids, string $tag): void
169
182
}
170
183
171
184
$ response = $ this ->client ->request ('POST ' , sprintf ('https://%s/api/tags/%s.json ' , $ this ->getEndpoint (), $ tag ), [
172
- 'headers ' => [
173
- 'Authorization ' => 'Loco ' .$ this ->apiKey ,
174
- ],
185
+ 'headers ' => $ this ->getDefaultHeaders (),
175
186
'body ' => $ idsAsString ,
176
187
]);
177
188
@@ -182,10 +193,8 @@ private function tagsAssets(array $ids, string $tag): void
182
193
183
194
private function createTag (string $ tag ): void
184
195
{
185
- $ response = $ this ->client ->request ('POST ' , sprintf ('https://%s/api/tags.json ' , $ this ->getEndpoint (), $ tag ), [
186
- 'headers ' => [
187
- 'Authorization ' => 'Loco ' .$ this ->apiKey ,
188
- ],
196
+ $ response = $ this ->client ->request ('POST ' , sprintf ('https://%s/api/tags.json ' , $ this ->getEndpoint ()), [
197
+ 'headers ' => $ this ->getDefaultHeaders (),
189
198
'body ' => [
190
199
'name ' => $ tag ,
191
200
],
@@ -199,9 +208,7 @@ private function createTag(string $tag): void
199
208
private function getTags (): array
200
209
{
201
210
$ response = $ this ->client ->request ('GET ' , sprintf ('https://%s/api/tags.json ' , $ this ->getEndpoint ()), [
202
- 'headers ' => [
203
- 'Authorization ' => 'Loco ' .$ this ->apiKey ,
204
- ],
211
+ 'headers ' => $ this ->getDefaultHeaders (),
205
212
]);
206
213
207
214
$ content = $ response ->getContent (false );
@@ -216,9 +223,7 @@ private function getTags(): array
216
223
private function deleteAsset (string $ id ): void
217
224
{
218
225
$ response = $ this ->client ->request ('DELETE ' , sprintf ('https://%s/api/assets/%s.json ' , $ this ->getEndpoint (), $ id ), [
219
- 'headers ' => [
220
- 'Authorization ' => 'Loco ' .$ this ->apiKey ,
221
- ],
226
+ 'headers ' => $ this ->getDefaultHeaders (),
222
227
]);
223
228
224
229
if (Response::HTTP_OK !== $ response ->getStatusCode ()) {
0 commit comments