8000 [Translation] remove credentials from PoEditorProvider · symfony/symfony@4373d7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 4373d7b

Browse files
[Translation] remove credentials from PoEditorProvider
1 parent 8ef49cf commit 4373d7b

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProviderFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public function create(Dsn $dsn): ProviderInterface
5050
throw new UnsupportedSchemeException($dsn, 'lokalise', $this->getSupportedSchemes());
5151
}
5252

53-
$endpoint = sprintf('%s%s', 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost(), $dsn->getPort() ? ':'.$dsn->getPort() : '');
53+
$endpoint = 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost();
54+
$endpoint .= $dsn->getPort() ? ':'.$dsn->getPort() : '';
55+
5456
$client = $this->client->withOptions([
5557
'base_uri' => 'https://'.$endpoint.'/projects/'.$this->getUser($dsn).'/api2/',
5658
'headers' => [
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Bridge\PoEditor;
13+
14+
use Symfony\Component\HttpClient\DecoratorTrait;
15+
use Symfony\Component\HttpClient\ScopingHttpClient;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
use Symfony\Contracts\HttpClient\ResponseInterface;
18+
19+
final class PoEditorHttpClient implements HttpClientInterface
20+
{
21+
use DecoratorTrait;
22+
23+
public function request(string $method, string $url, array $options = []): ResponseInterface
24+
{
25+
if (isset($options['poeditor_credentials'])) {
26+
if ('POST' === $method) {
27+
$options['body'] = $options['poeditor_credentials'] + $options['body'];
28+
}
29+
unset($options['poeditor_credentials']);
30+
}
31+
32+
return $this->client->request($method, $url, $options);
33+
}
34+
35+
public static function create(HttpClientInterface $client, string $baseUri, string $apiToken, string $projectId): HttpClientInterface
36+
{
37+
return ScopingHttpClient::forBaseUri(new self($client), $baseUri, [
38+
'poeditor_credentials' => [
39+
'api_token' => $apiToken,
40+
'id' => $projectId,
41+
],
42+
]);
43+
}
44+
}

src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,14 @@
3333
*/
3434
final class PoEditorProvider implements ProviderInterface
3535
{
36-
private $apiKey;
37-
private $projectId;
3836
private $client;
3937
private $loader;
4038
private $logger;
4139
private $defaultLocale;
4240
private $endpoint;
4341

44-
public function __construct(string $apiKey, string $projectId, HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint)
42+
public function __construct(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint)
4543
{
46-
$this->apiKey = $apiKey;
47-
$this->projectId = $projectId;
4844
$this->client = $client;
4945
$this->loader = $loader;
5046
$this->logger = $logger;
@@ -106,8 +102,6 @@ public function read(array $domains, array $locales): TranslatorBag
106102
foreach ($domains as $domain) {
107103
$response = $this->client->request('POST', 'projects/export', [
108104
'body' => [
109-
'api_token' => $this->apiKey,
110-
'id' => $this->projectId,
111105
'language' => $locale,
112106
'type' => 'xlf',
113107
'filters' => json_encode(['translated']),
@@ -176,8 +170,6 @@ private function addTerms(array $terms): void
176170
{
177171
$response = $this->client->request('POST', 'terms/add', [
178172
'body' => [
179-
'api_token' => $this->apiKey,
180-
'id' => $this->projectId,
181173
'data' => json_encode($terms),
182174
],
183175
]);
@@ -194,8 +186,6 @@ private function addTranslations(array $translationsPerLocale): void
194186
foreach ($translationsPerLocale as $locale => $translations) {
195187
$responses = $this->client->request('POST', 'translations/add', [
196188
'body' => [
197-
'api_token' => $this->apiKey,
198-
'id' => $this->projectId,
199189
'language' => $locale,
200190
'data' => F438 ; json_encode($translations),
201191
],
@@ -213,8 +203,6 @@ private function deleteTerms(array $ids): void
213203
{
214204
$response = $this->client->request('POST', 'terms/delete', [
215205
'body' => [
216-
'api_token' => $this->apiKey,
217-
'id' => $this->projectId,
218206
'data' => json_encode($ids),
219207
],
220208
]);

src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProviderFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public function create(Dsn $dsn): ProviderInterface
5050
throw new UnsupportedSchemeException($dsn, 'poeditor', $this->getSupportedSchemes());
5151
}
5252

53-
$endpoint = sprintf('%s%s', 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost(), $dsn->getPort() ? ':'.$dsn->getPort() : '');
54-
$client = $this->client->withOptions([
55-
'base_uri' => 'https://'.$endpoint.'/v2/',
56-
]);
53+
$endpoint = 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost();
54+
$endpoint .= $dsn->getPort() ? ':'.$dsn->getPort() : '';
5755

58-
return new PoEditorProvider($this->getPassword($dsn), $this->getUser($dsn), $client, $this->loader, $this->logger, $this->defaultLocale, $endpoint);
56+
$client = PoEditorHttpClient::create($this->client, 'https://'.$endpoint.'/v2/', $this->getPassword($dsn), $this->getUser($dsn));
57+
58+
return new PoEditorProvider($client, $this->loader, $this->logger, $this->defaultLocale, $endpoint);
5959
}
6060

6161
protected function getSupportedSchemes(): array

src/Symfony/Component/Translation/Bridge/PoEditor/Tests/PoEditorProviderTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psr\Log\LoggerInterface;
66
use Symfony\Component\HttpClient\MockHttpClient;
77
use Symfony\Component\HttpClient\Response\MockResponse;
8+
use Symfony\Component\Translation\Bridge\PoEditor\PoEditorHttpClient;
89
use Symfony\Component\Translation\Bridge\PoEditor\PoEditorProvider;
910
use Symfony\Component\Translation\Loader\ArrayLoader;
1011
use Symfony\Component\Translation\Loader\LoaderInterface;
@@ -19,29 +20,25 @@ class PoEditorProviderTest extends ProviderTestCase
1920
{
2021
public function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface
2122
{
22-
return new PoEditorProvider('API_KEY', 'PROJECT_ID', $client, $loader, $logger, $defaultLocale, $endpoint);
23+
return new PoEditorProvider(PoEditorHttpClient::create($client, 'https://poeditor', 'API_KEY', 'PROJECT_ID'), $loader, $logger, $defaultLocale, $endpoint);
2324
}
2425

2526
public function toStringProvider(): iterable
2627
{
28+
$client = PoEditorHttpClient::create($this->getClient(), 'https://poeditor', 'API_KEY', 'PROJECT_ID');
29+
2730
yield [
28-
$this->createProvider($this->getClient()->withOptions([
29-
'base_uri' => 'api.poeditor.com',
30-
]), $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'api.poeditor.com'),
31+
$this->createProvider($client, $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'api.poeditor.com'),
3132
'poeditor://api.poeditor.com',
3233
];
3334

3435
yield [
35-
$this->createProvider($this->getClient()->withOptions([
36-
'base_uri' => 'https://example.com',
37-
]), $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com'),
36+
$this->createProvider($client, $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com'),
3837
'poeditor://example.com',
3938
];
4039

4140
yield [
42-
$this->createProvider($this->getClient()->withOptions([
43-
'base_uri' => 'https://example.com:99',
44-
]), $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com:99'),
41+
$this->createProvider($client, $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com:99'),
4542
'poeditor://example.com:99',
4643
];
4744
}

0 commit comments

Comments
 (0)
0