Skip to content

Commit

Permalink
[TASK] Streamline glossary sync controller
Browse files Browse the repository at this point in the history
Update flashMassage for user interaction and denied calls with not right configuration.
  • Loading branch information
NarkNiro committed May 17, 2024
1 parent d0d7873 commit 01b7f2f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 42 deletions.
5 changes: 0 additions & 5 deletions Build/phpstan/Core12/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ parameters:
count: 1
path: ../../../Classes/Service/DeeplGlossaryService.php

-
message: "#^PHPDoc tag @throws with type Doctrine\\\\DBAL\\\\DBALException\\|Doctrine\\\\DBAL\\\\Driver\\\\Exception\\|Doctrine\\\\DBAL\\\\Exception\\|TYPO3\\\\CMS\\\\Core\\\\Exception\\\\SiteNotFoundException is not subtype of Throwable$#"
count: 1
path: ../../../Classes/Service/DeeplGlossaryService.php

-
message: "#^Offset 'pid' does not exist on array\\|null\\.$#"
count: 1
Expand Down
75 changes: 47 additions & 28 deletions Classes/Controller/GlossarySyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@
namespace WebVision\WvDeepltranslate\Controller;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use WebVision\WvDeepltranslate\Exception\FailedToCreateGlossaryException;
use WebVision\WvDeepltranslate\Exception\InvalidArgumentException;
use WebVision\WvDeepltranslate\Service\DeeplGlossaryService;

class GlossarySyncController
{
protected DeeplGlossaryService $deeplGlossaryService;

private FlashMessageService $flashMessageService;

public function __construct(
DeeplGlossaryService $deeplGlossaryService
DeeplGlossaryService $deeplGlossaryService,
FlashMessageService $flashMessageService
) {
$this->deeplGlossaryService = $deeplGlossaryService;
$this->flashMessageService = $flashMessageService;
}

/**
Expand All @@ -33,36 +39,49 @@ public function update(ServerRequestInterface $request): RedirectResponse
$processingParameters = $request->getQueryParams();

if (!isset($processingParameters['uid'])) {
throw new InvalidArgumentException(
'No ID given for glossary synchronization',
1676935668643
);
$this->flashMessageService
->getMessageQueueByIdentifier()
->enqueue((new FlashMessage(
'No ID given for glossary synchronization',
'',
2,
true
)));
return new RedirectResponse($processingParameters['returnUrl']);
}

$this->deeplGlossaryService->syncGlossaries((int)$processingParameters['uid']);

if ((new \TYPO3\CMS\Core\Information\Typo3Version())->getMajorVersion() >= 12) {
$severity = \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::OK;
} else {
$severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK;
// Check page configuration of glossary type
/** @var array{uid: int, doktype: string|int, module: string} $pages */
$pages = BackendUtility::getRecord('pages', (int)$processingParameters['uid']);
if ((int)$pages['doktype'] !== PageRepository::DOKTYPE_SYSFOLDER && $pages['module'] !== 'glossary') {
$this->flashMessageService->getMessageQueueByIdentifier()->enqueue(new FlashMessage(
sprintf('Page "%d" not configured for glossary synchronization.', $pages['uid']),
(string)LocalizationUtility::translate(
'glossary.sync.title.invalid',
'wv_deepltranslate'
),
2,
true
));
return new RedirectResponse($processingParameters['returnUrl']);
}
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
(string)LocalizationUtility::translate(
'glossary.sync.message',
'wv_deepltranslate'
),
(string)LocalizationUtility::translate(
'glossary.sync.title',
'wv_deepltranslate'
),
$severity,
true
);

GeneralUtility::makeInstance(FlashMessageService::class)
->getMessageQueueByIdentifier()
->enqueue($flashMessage);
try {
$this->deeplGlossaryService->syncGlossaries((int)$processingParameters['uid']);
$this->flashMessageService->getMessageQueueByIdentifier()->enqueue(new FlashMessage(
(string)LocalizationUtility::translate('glossary.sync.message', 'wv_deepltranslate'),
(string)LocalizationUtility::translate('glossary.sync.title', 'wv_deepltranslate'),
0, // OK
true
));
} catch (FailedToCreateGlossaryException $exception) {
$this->flashMessageService->getMessageQueueByIdentifier()->enqueue(new FlashMessage(
(string)LocalizationUtility::translate('glossary.sync.message.invalid', 'wv_deepltranslate'),
(string)LocalizationUtility::translate('glossary.sync.title.invalid', 'wv_deepltranslate'),
2, // Error
true
));
}

return new RedirectResponse($processingParameters['returnUrl']);
}
Expand Down
10 changes: 10 additions & 0 deletions Classes/Exception/FailedToCreateGlossaryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace WebVision\WvDeepltranslate\Exception;

final class FailedToCreateGlossaryException extends \Exception
{

}
12 changes: 8 additions & 4 deletions Classes/Service/DeeplGlossaryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use DeepL\GlossaryEntries;
use DeepL\GlossaryInfo;
use DeepL\GlossaryLanguagePair;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use WebVision\WvDeepltranslate\ClientInterface;
use WebVision\WvDeepltranslate\Domain\Repository\GlossaryRepository;
use WebVision\WvDeepltranslate\Exception\FailedToCreateGlossaryException;
use WebVision\WvDeepltranslate\Exception\GlossaryEntriesNotExistException;

final class DeeplGlossaryService
Expand Down Expand Up @@ -125,13 +125,17 @@ public function getPossibleGlossaryLanguageConfig(): array
/**
* @throws Exception
* @throws SiteNotFoundException
* @throws DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function syncGlossaries(int $uid): void
{
$glossaries = $this->glossaryRepository
->getGlossaryInformationForSync($uid);
$glossaries = $this->glossaryRepository->getGlossaryInformationForSync($uid);
if (empty($glossaries)) {
throw new FailedToCreateGlossaryException(
'Glossary can not created, the TYPO3 information are invalide.',
1714987594661
);
}

foreach ($glossaries as $glossaryInformation) {
if ($glossaryInformation['glossary_id'] !== '') {
Expand Down
16 changes: 11 additions & 5 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,23 @@
<source>Create new Glossary translation</source>
</trans-unit>

<trans-unit id="glossary.sync.title">
<source>DeepL sync ready</source>
</trans-unit>
<trans-unit id="glossary.sync.message">
<source>Synchronization with DeepL is done. Your glossaries are ready to use.</source>
</trans-unit>
<trans-unit id="glossary.sync.title">
<source>DeepL Sync ready</source>
<trans-unit id="glossary.sync.title.invalid">
<source>DeepL glossary sync invalid</source>
</trans-unit>
<trans-unit id="glossary.not-sync.message">
<source>Glossary synchronization with DeepL is pending. Your glossaries are not operational.</source>
<trans-unit id="glossary.sync.message.invalid">
<source>Glossary synchronization with DeepL error. Your glossaries has no entries or is invalid.</source>
</trans-unit>
<trans-unit id="glossary.not-sync.title">
<source>DeepL Glossary is out of Sync</source>
<source>DeepL glossary is out of sync</source>
</trans-unit>
<trans-unit id="glossary.not-sync.message">
<source>Glossary synchronization with DeepL is pending. Your glossaries are not operational.</source>
</trans-unit>

<trans-unit id="glossaryentry">
Expand Down

0 comments on commit 01b7f2f

Please sign in to comment.