Skip to content

Commit

Permalink
wrap Deepl PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Apr 6, 2018
1 parent e793029 commit 46a137e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
5 changes: 3 additions & 2 deletions bundle/Resources/doc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ ez_platform_automated_translation:
default:
configurations:
google:
apiKey: "xxx"

apiKey: "google-api-key"
deepl:
authKey: "deepl-pro-key"
```
17 changes: 12 additions & 5 deletions lib/Client/Deepl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
declare(strict_types=1);

namespace EzSystems\EzPlatformAutomatedTranslation\Client;

use EzSystems\EzPlatformAutomatedTranslation\Exception\ClientNotConfiguredException;
use EzSystems\EzPlatformAutomatedTranslation\Exception\InvalidLanguageCodeException;
use GuzzleHttp\Client;
Expand All @@ -20,6 +21,9 @@
*/
class Deepl implements ClientInterface
{
/**
* @var string
*/
private $authKey;

/**
Expand Down Expand Up @@ -67,7 +71,7 @@ public function translate(string $payload, ?string $from, string $to): string
];
}

$http = new Client(
$http = new Client(
[
'base_uri' => 'https://api.deepl.com',
'timeout' => 5.0,
Expand All @@ -85,17 +89,20 @@ public function translate(string $payload, ?string $from, string $to): string
*/
public function supportsLanguage(string $languageCode)
{
return in_array($this->normalized($languageCode), self::LANGUAGE_CODES);
return \in_array($this->normalized($languageCode), self::LANGUAGE_CODES);
}

/**
* {@inheritdoc}
*/
private function normalized(string $languageCode): string
{
if (in_array($languageCode, self::LANGUAGE_CODES)) {
if (\in_array($languageCode, self::LANGUAGE_CODES)) {
return $languageCode;
}

$code = strtoupper(substr($languageCode, 0, 2));
if (in_array($code, self::LANGUAGE_CODES)) {
if (\in_array($code, self::LANGUAGE_CODES)) {
return $code;
}

Expand All @@ -105,5 +112,5 @@ private function normalized(string $languageCode): string
/**
* Google List of available code https://www.deepl.com/api.html.
*/
private const LANGUAGE_CODES = ["EN", "DE", "FR", "ES", "IT", "NL", "PL"];
private const LANGUAGE_CODES = ['EN', 'DE', 'FR', 'ES', 'IT', 'NL', 'PL'];
}
9 changes: 4 additions & 5 deletions lib/Client/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace EzSystems\EzPlatformAutomatedTranslation\Client;

use EzSystems\EzPlatformAutomatedTranslation\Exception\ClientNotConfiguredException;
use EzSystems\EzPlatformAutomatedTranslation\Exception\InvalidLanguageCodeException;
use GuzzleHttp\Client;

/**
Expand Down Expand Up @@ -46,9 +48,7 @@ public function getServiceFullName(): string
public function setConfiguration(array $configuration): void
{
if (!isset($configuration['apiKey'])) {
throw new \RuntimeException(
'Remote Translation service ' . self::class . ' cannot autoconfigured without apiKey'
);
throw new ClientNotConfiguredException('authKey is required');
}
$this->apiKey = $configuration['apiKey'];
}
Expand Down Expand Up @@ -111,8 +111,7 @@ private function normalized(string $languageCode): string
if ('zh_TW' === $languageCode) {
return 'zh-TW';
}

return $languageCode;
throw new InvalidLanguageCodeException($languageCode, $this->getServiceAlias());
}

/**
Expand Down
16 changes: 15 additions & 1 deletion lib/Exception/ClientNotConfiguredException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
<?php
/**
* eZ Automated Translation Bundle.
*
* @package EzSystems\eZAutomatedTranslationBundle
*
* @author Novactive <s.morel@novactive.com>
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAutomatedTranslation\Exception;

use RuntimeException;

/**
* Class ClientNotConfiguredException.
*/
class ClientNotConfiguredException extends RuntimeException
{
}
}
24 changes: 22 additions & 2 deletions lib/Exception/InvalidLanguageCodeException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
<?php
/**
* eZ Automated Translation Bundle.
*
* @package EzSystems\eZAutomatedTranslationBundle
*
* @author Novactive <s.morel@novactive.com>
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAutomatedTranslation\Exception;

use InvalidArgumentException;

/**
* Class InvalidLanguageCodeException.
*/
class InvalidLanguageCodeException extends InvalidArgumentException
{
public function __construct($languageCode, $driver)
/**
* InvalidLanguageCodeException constructor.
*
* @param string $languageCode
* @param string $driver
*/
public function __construct(string $languageCode, string $driver)
{
parent::__construct("$languageCode not recognized by $driver");
}
}
}

0 comments on commit 46a137e

Please sign in to comment.