Skip to content
Draft
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
2 changes: 1 addition & 1 deletion Build/php-cs-fixer/php-cs-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
'no_empty_phpdoc' => true,
'no_null_property_initialization' => true,
'php_unit_mock_short_will_return' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'static'],
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
53 changes: 0 additions & 53 deletions Classes/AbstractClient.php

This file was deleted.

48 changes: 34 additions & 14 deletions Classes/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace WebVision\Deepltranslate\Core;

use DeepL\DeepLClient;
use DeepL\DeepLClientOptions;
use DeepL\DeepLException;
use DeepL\GlossaryEntries;
use DeepL\GlossaryInfo;
Expand All @@ -12,13 +14,31 @@
use DeepL\TextResult;
use DeepL\TranslateTextOptions;
use DeepL\Usage;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Http\Client\GuzzleClientFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use WebVision\Deepltranslate\Core\Exception\ApiKeyNotSetException;

/**
* @internal No public usage
* @todo split the client into two separate services?
*/
final class Client extends AbstractClient
final class Client implements TranslatorInterface, UsageInterface
{
private ?DeepLClient $translator;
public function __construct(
private readonly ConfigurationInterface $configuration,
private readonly LoggerInterface $logger
) {
if ($this->configuration->getApiKey() === '') {
$this->logger->notice('DeepL API key is not set. DeepL translation is disabled');
$this->translator = null;
return;
}
$options[DeepLClientOptions::HTTP_CLIENT] = GeneralUtility::makeInstance(GuzzleClientFactory::class)->getClient();
$this->translator = new DeepLClient($this->configuration->getApiKey(), $options);
}

/**
* @return TextResult|TextResult[]|null
*
Expand All @@ -30,7 +50,7 @@ public function translate(
string $targetLang,
string $glossary = '',
string $formality = ''
) {
): array|TextResult|null {
$options = [
TranslateTextOptions::FORMALITY => $formality ?: 'default',
TranslateTextOptions::TAG_HANDLING => 'xml',
Expand All @@ -41,7 +61,7 @@ public function translate(
}

try {
return $this->getTranslator()->translateText(
return $this->translator?->translateText(
$content,
$sourceLang,
$targetLang,
Expand All @@ -66,9 +86,9 @@ public function translate(
public function getSupportedLanguageByType(string $type = 'target'): array
{
try {
return ($type === 'target')
? $this->getTranslator()->getTargetLanguages()
: $this->getTranslator()->getSourceLanguages();
return (($type === 'target')
? $this->translator?->getTargetLanguages()
: $this->translator?->getSourceLanguages()) ?? [];
} catch (DeepLException $exception) {
$this->logger->error(sprintf(
'%s (%d)',
Expand All @@ -88,7 +108,7 @@ public function getSupportedLanguageByType(string $type = 'target'): array
public function getGlossaryLanguagePairs(): array
{
try {
return $this->getTranslator()->getGlossaryLanguages();
return $this->translator?->getGlossaryLanguages() ?? [];
} catch (DeepLException $exception) {
$this->logger->error(sprintf(
'%s (%d)',
Expand All @@ -108,7 +128,7 @@ public function getGlossaryLanguagePairs(): array
public function getAllGlossaries(): array
{
try {
return $this->getTranslator()->listGlossaries();
return $this->translator?->listGlossaries() ?? [];
} catch (DeepLException $exception) {
$this->logger->error(sprintf(
'%s (%d)',
Expand All @@ -126,7 +146,7 @@ public function getAllGlossaries(): array
public function getGlossary(string $glossaryId): ?GlossaryInfo
{
try {
return $this->getTranslator()->getGlossary($glossaryId);
return $this->translator?->getGlossary($glossaryId);
} catch (DeepLException $exception) {
$this->logger->error(sprintf(
'%s (%d)',
Expand Down Expand Up @@ -164,12 +184,12 @@ public function createGlossary(
$prepareEntriesForGlossary[$source] = $target;
}
try {
return $this->getTranslator()->createGlossary(
return $this->translator?->createGlossary(
$glossaryName,
$sourceLang,
$targetLang,
GlossaryEntries::fromEntries($prepareEntriesForGlossary)
);
) ?? throw new DeepLException();
} catch (DeepLException $e) {
return new GlossaryInfo(
'',
Expand All @@ -189,7 +209,7 @@ public function createGlossary(
public function deleteGlossary(string $glossaryId): void
{
try {
$this->getTranslator()->deleteGlossary($glossaryId);
$this->translator?->deleteGlossary($glossaryId);
} catch (DeepLException $exception) {
$this->logger->error(sprintf(
'%s (%d)',
Expand All @@ -205,7 +225,7 @@ public function deleteGlossary(string $glossaryId): void
public function getGlossaryEntries(string $glossaryId): ?GlossaryEntries
{
try {
return $this->getTranslator()->getGlossaryEntries($glossaryId);
return $this->translator?->getGlossaryEntries($glossaryId);
} catch (DeepLException $exception) {
$this->logger->error(sprintf(
'%s (%d)',
Expand All @@ -223,7 +243,7 @@ public function getGlossaryEntries(string $glossaryId): ?GlossaryEntries
public function getUsage(): ?Usage
{
try {
return $this->getTranslator()->getUsage();
return $this->translator?->getUsage();
} catch (DeepLException $exception) {
$this->logger->error(sprintf(
'%s (%d)',
Expand Down
17 changes: 17 additions & 0 deletions Classes/Client/DeepLAPIClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace WebVision\Deepltranslate\Core\Client;

use DeepL\DeepLClient;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use WebVision\Deepltranslate\Core\Client\DeepLClientInterface;

/**
* Wrapper class for the DeepL PHP API.
*/
#[AsAlias(id: DeepLClientInterface::class)]
final class DeepLAPIClient extends DeepLClient implements DeepLClientInterface
{
}
14 changes: 14 additions & 0 deletions Classes/Client/DeepLClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace WebVision\Deepltranslate\Core\Client;

use WebVision\Deepltranslate\Core\ConfigurationInterface;

final class DeepLClientFactory
{
public function __construct(private readonly ConfigurationInterface $configuration)
{
}
}
Loading