From ecc2d3d013cd2b9cb8b47eb6a0e5b96008a7d0e1 Mon Sep 17 00:00:00 2001 From: Markus Hofmann Date: Fri, 28 Apr 2023 17:45:35 +0200 Subject: [PATCH] [TASK] Ensure, TYPO3 is not running in CLI mode As the method deleteGlossary is called both in backend and CLI context, the Exception should be thrown in CLI context, but generate a FlashMessage in backend context. --- Classes/Service/DeeplGlossaryService.php | 28 ++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Classes/Service/DeeplGlossaryService.php b/Classes/Service/DeeplGlossaryService.php index 4349346d..d4482830 100644 --- a/Classes/Service/DeeplGlossaryService.php +++ b/Classes/Service/DeeplGlossaryService.php @@ -8,6 +8,7 @@ use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; +use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Messaging\FlashMessageService; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -155,7 +156,7 @@ public function createGlossary( * * @throws DeepLException */ - public function deleteGlossary(string $glossaryId) + public function deleteGlossary(string $glossaryId): ?array { $url = $this->client->buildBaseUrl(self::API_URL_SUFFIX_GLOSSARIES); $url .= "/$glossaryId"; @@ -164,17 +165,22 @@ public function deleteGlossary(string $glossaryId) $this->client->request($url, '', 'DELETE'); } catch (BadResponseException $e) { // FlashMessage($message, $title, $severity = self::OK, $storeInSession) - $message = GeneralUtility::makeInstance( - FlashMessage::class, - $e->getMessage(), - 'DeepL Api', - FlashMessage::WARNING, - true - ); - $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); - $messageQueue = $flashMessageService->getMessageQueueByIdentifier(); - $messageQueue->addMessage($message); + if (Environment::isCli()) { + throw $e; + }else { + $message = GeneralUtility::makeInstance( + FlashMessage::class, + $e->getMessage(), + 'DeepL Api', + FlashMessage::WARNING, + true + ); + $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); + $messageQueue = $flashMessageService->getMessageQueueByIdentifier(); + $messageQueue->addMessage($message); + } } + return null; } /**