Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4858bfa

Browse filesBrowse files
minor #64139 [Translation] Improve LocoProvider::delete (MatTheCat)
This PR was merged into the 8.1 branch. Discussion ---------- [Translation] Improve `LocoProvider::delete` | Q | A | ------------- | --- | Branch? | 8.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | N/A | License | MIT The previous implementation deleted every asset from the default locale’s catalogue domains. This PR deletes every asset present in the catalogue, which should speed things quite a bit. Commits ------- 3d54c0b [Translation] Improve `LocoProvider::delete`
2 parents 071a33c + 3d54c0b commit 4858bfa
Copy full SHA for 4858bfa

2 files changed

+27-34Lines changed: 27 additions & 34 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php
+27-13Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,40 @@ public function read(array $domains, array $locales): TranslatorBag
184184

185185
public function delete(TranslatorBagInterface $translatorBag): void
186186
{
187-
$catalogue = $translatorBag->getCatalogue($this->defaultLocale);
187+
$responses = new \SplObjectStorage();
188+
$deletedIds = [];
188189

189-
$responses = [];
190+
foreach ($translatorBag->getCatalogues() as $catalogue) {
191+
foreach ($catalogue->all() as $domain => $messages) {
192+
foreach ($messages as $key => $message) {
193+
$id = $domain.'__'.$key;
194+
if (isset($deletedIds[$id])) {
195+
continue;
196+
}
190197

191-
foreach (array_keys($catalogue->all()) as $domain) {
192-
foreach ($this->getAssetsIds($domain) as $id) {
193-
$responses[$id] = $this->client->request('DELETE', \sprintf('assets/%s.json', rawurlencode($id)));
198+
$responses[$this->client->request('DELETE', \sprintf('assets/%s.json', rawurlencode($id)))] = $id;
199+
$deletedIds[$id] = $id;
200+
}
194201
}
195202
}
196203

197-
foreach ($responses as $key => $response) {
198-
if (403 === $statusCode = $response->getStatusCode()) {
199-
$this->logger->error('The API key used does not have sufficient permissions to delete assets.');
200-
}
204+
foreach ($this->client->stream($responses) as $response => $chunk) {
205+
if ($chunk->isFirst()) {
206+
switch ($response->getStatusCode()) {
207+
case 403:
208+
$this->logger->error('The API key used does not have sufficient permissions to delete assets.');
209+
// no break
210+
case 200:
211+
case 404:
212+
$response->cancel();
213+
}
214+
} elseif ($chunk->isLast()) {
215+
$assetId = $responses[$response];
201216

202-
if (200 !== $statusCode && 404 !== $statusCode) {
203-
$this->logger->error(\sprintf('Unable to delete translation key "%s" to Loco: "%s".', $key, $response->getContent(false)));
217+
$this->logger->error(\sprintf('Unable to delete translation key "%s" to Loco: "%s".', $assetId, $response->getContent(false)));
204218

205-
if (500 <= $statusCode) {
206-
throw new ProviderException(\sprintf('Unable to delete translation key "%s" to Loco.', $key), $response);
219+
if (500 <= $response->getStatusCode()) {
220+
throw new ProviderException(\sprintf('Unable to delete translation key "%s" to Loco.', $assetId), $response);
207221
}
208222
}
209223
}
Collapse file

‎src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderTest.php
-21Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -839,26 +839,12 @@ public function testDeleteProcess()
839839

840840
$provider = self::createProvider(
841841
new MockHttpClient([
842-
function (string $method, string $url, array $options = []): ResponseInterface {
843-
$this->assertSame('GET', $method);
844-
$this->assertSame('https://localise.biz/api/assets?filter=messages', $url);
845-
$this->assertSame(['filter' => 'messages'], $options['query']);
846-
847-
return new MockResponse('[{"id":"messages__a"}]');
848-
},
849842
function (string $method, string $url): MockResponse {
850843
$this->assertSame('DELETE', $method);
851844
$this->assertSame('https://localise.biz/api/assets/messages__a.json', $url);
852845

853846
return new MockResponse();
854847
},
855-
function (string $method, string $url, array $options = []): ResponseInterface {
856-
$this->assertSame('GET', $method);
857-
$this->assertSame('https://localise.biz/api/assets?filter=validators', $url);
858-
$this->assertSame(['filter' => 'validators'], $options['query']);
859-
860-
return new MockResponse('[{"id":"validators__post.num_comments"}]');
861-
},
862848
function (string $method, string $url): MockResponse {
863849
$this->assertSame('DELETE', $method);
864850
$this->assertSame('https://localise.biz/api/assets/validators__post.num_comments.json', $url);
@@ -884,13 +870,6 @@ public function testDeleteServerError()
884870

885871
$provider = self::createProvider(
886872
new MockHttpClient([
887-
function (string $method, string $url, array $options = []): ResponseInterface {
888-
$this->assertSame('GET', $method);
889-
$this->assertSame('https://localise.biz/api/assets?filter=messages', $url);
890-
$this->assertSame(['filter' => 'messages'], $options['query']);
891-
892-
return new MockResponse('[{"id":"messages__a"}]');
893-
},
894873
function (string $method, string $url): MockResponse {
895874
$this->assertSame('DELETE', $method);
896875
$this->assertSame('https://localise.biz/api/assets/messages__a.json', $url);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.