Skip to content

Commit

Permalink
[TASK] Streamline number formatting to UsageService
Browse files Browse the repository at this point in the history
For better maintenance move the severity calculation and number
formatting to the UsageService and adjust code.
  • Loading branch information
calien666 committed Nov 20, 2024
1 parent 86ac206 commit 30bd64c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 71 deletions.
41 changes: 5 additions & 36 deletions Classes/Event/Listener/UsageToolBarEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ public function __invoke(SystemInformationToolbarCollectorEvent $systemInformati
'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.toolbar.message'
);

$severity = $this->determineSeverity($usage->character->count, $usage->character->limit);
$severity = $this->usageService->determineSeverity($usage->character->count, $usage->character->limit);

$systemInformation->getToolbarItem()->addSystemInformation(
$title,
sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)),
sprintf(
$message,
$this->usageService->formatNumber($usage->character->count),
$this->usageService->formatNumber($usage->character->limit)),
'actions-localize-deepl',
$severity
);
Expand All @@ -63,38 +66,4 @@ private function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}

/**
* Make large API limits easier to read
*
* @param int $number Any large integer - 5000000
* @return string Formated, better readable string variant of the integer - 5.000.000
*/
private function formatNumber(int $number): string
{
return number_format($number, 0, ',', '.');
}


/**
* Calculate the message severity based on the quota usage rate
*
* @param int $characterCount Already translated characters in the current billing period
* @param int $characterLimit Total character limit in the current billing period
* @return string Severity level
*/
private function determineSeverity(int $characterCount, int $characterLimit): string
{
$quotaUtilization = ($characterCount / $characterLimit) * 100;
if ($quotaUtilization >= 100) {
return InformationStatus::STATUS_ERROR;
}
if ($quotaUtilization >= 98) {
return InformationStatus::STATUS_WARNING;
}
if ($quotaUtilization >= 90) {
return InformationStatus::STATUS_INFO;
}
return InformationStatus::STATUS_NOTICE;
}
}
42 changes: 7 additions & 35 deletions Classes/Hooks/UsageProcessAfterFinishHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace WebVision\WvDeepltranslate\Hooks;

use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
Expand Down Expand Up @@ -46,7 +48,7 @@ public function processCmdmap_afterFinish(DataHandler $dataHandler): void
'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.flashmassage.limit.description'
);

$severity = $this->determineSeverity($usage->character->count, $usage->character->limit);
$severity = $this->usageService->determineSeverity($usage->character->count, $usage->character->limit);
// Reduce noise - Don't bother editors with low quota usage messages
if ($severity === FlashMessage::NOTICE) {
return;
Expand All @@ -57,7 +59,10 @@ public function processCmdmap_afterFinish(DataHandler $dataHandler): void

$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)),
sprintf(
$message,
$this->usageService->formatNumber($usage->character->count) ?: $usage->character->count,
$this->usageService->formatNumber($usage->character->limit)) ?: $usage->character->limit,
$title,
$severity,
true
Expand All @@ -70,37 +75,4 @@ private function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}

/**
* Make large API limits easier to read
*
* @param int $number Any large integer - 5000000
* @return string Formated, better readable string variant of the integer - 5.000.000
*/
private function formatNumber(int $number): string
{
return number_format($number, 0, ',', '.');
}

/**
* Calculate the message severity based on the quota usage rate
*
* @param int $characterCount Already translated characters in the current billing period
* @param int $characterLimit Total character limit in the current billing period
* @return int Severity level
*/
private function determineSeverity(int $characterCount, int $characterLimit): int
{
$quotaUtilization = ($characterCount / $characterLimit) * 100;
if ($quotaUtilization >= 100) {
return FlashMessage::ERROR;
}
if ($quotaUtilization >= 98) {
return FlashMessage::WARNING;
}
if ($quotaUtilization >= 90) {
return FlashMessage::INFO;
}
return FlashMessage::NOTICE;
}
}
53 changes: 53 additions & 0 deletions Classes/Service/UsageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace WebVision\WvDeepltranslate\Service;

use DeepL\Usage;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use WebVision\WvDeepltranslate\ClientInterface;

final class UsageService implements UsageServiceInterface
Expand Down Expand Up @@ -47,4 +50,54 @@ public function isTranslateLimitExceeded(): bool
}
return $usage->character->count >= $usage->character->limit;
}

/**
* Make large API limits easier to read
*
* @param int $number Any large integer - 5000000
* @return string|false Formated, better readable string variant of the integer - 5.000.000
*/
public function formatNumber(int $number)
{
// @todo typo3/cms-core:>=12 remove polyfill switch, as php-intl is
// then a hard requirement and the polyfill is not needed
if ((new Typo3Version())->getMajorVersion() <= 12 && !extension_loaded('intl')) {
// TYPO3 v11 has a Symfony Polyfill, but this ony allows
// locale en or null. So we call without any parameter.
if (!class_exists(\NumberFormatter::class)) {
return number_format($number);
}
$numberFormatter = new \NumberFormatter();
} else {
$numberFormatter = new \NumberFormatter($this->getBackendUser()->uc['lang'] ?? 'en');
}
return $numberFormatter->format($number);
}

/**
* Calculate the message severity based on the quota usage rate
*
* @param int $characterCount Already translated characters in the current billing period
* @param int $characterLimit Total character limit in the current billing period
* @return int Severity level
*/
public function determineSeverity(int $characterCount, int $characterLimit): int
{
$quotaUtilization = ($characterCount / $characterLimit) * 100;
if ($quotaUtilization >= 100) {
return FlashMessage::ERROR;
}
if ($quotaUtilization >= 98) {
return FlashMessage::WARNING;
}
if ($quotaUtilization >= 90) {
return FlashMessage::INFO;
}
return FlashMessage::NOTICE;
}

private function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
}

0 comments on commit 30bd64c

Please sign in to comment.