Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete keys bug #15

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/PhraseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,29 +165,30 @@ public function write(TranslatorBagInterface $translatorBag): void

public function delete(TranslatorBagInterface $translatorBag): void
{
$defaultCatalogue = $translatorBag->getCatalogue($this->defaultLocale);
$keys = [[]];

/** @var string $domain */
foreach ($defaultCatalogue->getDomains() as $domain) {
/** @var string[] $keys */
if ([] === $keys = array_keys($defaultCatalogue->all($domain))) {
continue;
foreach ($translatorBag->getCatalogues() as $catalogue) {
/** @var string $domain */
foreach ($catalogue->getDomains() as $domain) {
/* @var string[] $keys */
$keys[] = array_keys($catalogue->all($domain));
}
}

$names = array_map(static fn ($v): ?string => preg_replace('/([\s:,])/', '\\\\\\\\$1', $v), $keys);
$keys = array_unique(array_merge(...$keys));
$names = array_map(static fn ($v): ?string => preg_replace('/([\s:,])/', '\\\\\\\\$1', $v), $keys);

foreach ($names as $name) {
$response = $this->httpClient->request('DELETE', 'keys', [
'query' => [
'q' => 'name:' . $name,
],
]);
foreach ($names as $name) {
$response = $this->httpClient->request('DELETE', 'keys', [
'query' => [
'q' => 'name:' . $name,
],
]);

if (200 !== $statusCode = $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to delete key "%s" in phrase: "%s".', $name, $response->getContent(false)));
if (200 !== $statusCode = $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to delete key "%s" in phrase: "%s".', $name, $response->getContent(false)));

$this->throwProviderException($statusCode, $response, 'Unable to delete key in phrase.');
}
$this->throwProviderException($statusCode, $response, 'Unable to delete key in phrase.');
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/PhraseProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,29 @@ public function testDelete(): void
],
]));

$bag->addCatalogue(new MessageCatalogue('de', [
'validators' => [],
'messages' => [
'another:erroneous:key' => 'value to delete',
'delete this,erroneous:key' => 'translated value',
],
]));

$responses = [
'delete key' => function (string $method, string $url): ResponseInterface {
'delete key one' => function (string $method, string $url): ResponseInterface {
$this->assertSame('DELETE', $method);
// https://api.phrase.com/api/v2/projects/1/keys?q=name:delete\\ this\\,erroneous\\:key
$this->assertSame('https://api.phrase.com/api/v2/projects/1/keys?q=name%3Adelete%5C%5C%20this%5C%5C%2Cerroneous%5C%5C%3Akey', $url);

return new MockResponse('', [
'http_code' => 200,
]);
},
'delete key two' => function (string $method, string $url): ResponseInterface {
$this->assertSame('DELETE', $method);
// 'https://api.phrase.com/api/v2/projects/1/keys?q=name:another\\:erroneous\\:key'
$this->assertSame('https://api.phrase.com/api/v2/projects/1/keys?q=name%3Aanother%5C%5C%3Aerroneous%5C%5C%3Akey', $url);

return new MockResponse('', [
'http_code' => 200,
]);
Expand Down