From 46a137e5f4609921fad47ec6ff469a37e72b9bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20Se=CC=81bastien?= Date: Fri, 6 Apr 2018 09:55:37 -0700 Subject: [PATCH] wrap Deepl PR --- bundle/Resources/doc/USAGE.md | 5 ++-- lib/Client/Deepl.php | 17 +++++++++---- lib/Client/Google.php | 9 ++++--- .../ClientNotConfiguredException.php | 16 ++++++++++++- .../InvalidLanguageCodeException.php | 24 +++++++++++++++++-- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/bundle/Resources/doc/USAGE.md b/bundle/Resources/doc/USAGE.md index 6f3adfb..c635efd 100644 --- a/bundle/Resources/doc/USAGE.md +++ b/bundle/Resources/doc/USAGE.md @@ -12,7 +12,8 @@ ez_platform_automated_translation: default: configurations: google: - apiKey: "xxx" - + apiKey: "google-api-key" + deepl: + authKey: "deepl-pro-key" ``` diff --git a/lib/Client/Deepl.php b/lib/Client/Deepl.php index 04aa71c..42cbcd4 100644 --- a/lib/Client/Deepl.php +++ b/lib/Client/Deepl.php @@ -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; @@ -20,6 +21,9 @@ */ class Deepl implements ClientInterface { + /** + * @var string + */ private $authKey; /** @@ -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, @@ -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; } @@ -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']; } diff --git a/lib/Client/Google.php b/lib/Client/Google.php index 0fdd94d..8835d68 100644 --- a/lib/Client/Google.php +++ b/lib/Client/Google.php @@ -12,6 +12,8 @@ namespace EzSystems\EzPlatformAutomatedTranslation\Client; +use EzSystems\EzPlatformAutomatedTranslation\Exception\ClientNotConfiguredException; +use EzSystems\EzPlatformAutomatedTranslation\Exception\InvalidLanguageCodeException; use GuzzleHttp\Client; /** @@ -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']; } @@ -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()); } /** diff --git a/lib/Exception/ClientNotConfiguredException.php b/lib/Exception/ClientNotConfiguredException.php index 898225b..5810bc3 100644 --- a/lib/Exception/ClientNotConfiguredException.php +++ b/lib/Exception/ClientNotConfiguredException.php @@ -1,8 +1,22 @@ + * @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 { -} \ No newline at end of file +} diff --git a/lib/Exception/InvalidLanguageCodeException.php b/lib/Exception/InvalidLanguageCodeException.php index 97963b5..816df2e 100644 --- a/lib/Exception/InvalidLanguageCodeException.php +++ b/lib/Exception/InvalidLanguageCodeException.php @@ -1,12 +1,32 @@ + * @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"); } -} \ No newline at end of file +}