TranslationProgressCalculator.php in TMGMT Translator Smartling 8.3
File
src/Smartling/TranslationProgressCalculator.php
View source
<?php
namespace Drupal\tmgmt_smartling\Smartling;
use Psr\Log\LoggerInterface;
class TranslationProgressCalculator {
private $totalKey = "totalStringCount";
private $authorizedKey = "authorizedStringCount";
private $completedKey = "completedStringCount";
private $excludedKey = "excludedStringCount";
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function isValid(array $data) {
$data_keys = array_keys($data);
if (!in_array($this->totalKey, $data_keys) || !in_array($this->authorizedKey, $data_keys) || !in_array($this->completedKey, $data_keys) || !in_array($this->excludedKey, $data_keys)) {
return FALSE;
}
return TRUE;
}
public function calculate(array $data, $auto_authorize_enabled = TRUE) {
if (!$this
->isValid($data)) {
throw new \InvalidArgumentException("Invalid input data: " . json_encode($data));
}
$total = intval($data[$this->totalKey]);
$authorized = intval($data[$this->authorizedKey]);
$completed = intval($data[$this->completedKey]);
$excluded = intval($data[$this->excludedKey]);
$unauthorized = $total - ($authorized + $completed + $excluded);
if ($authorized + $completed > 0) {
if ($auto_authorize_enabled) {
$progress = $completed / ($authorized + $completed);
}
else {
$progress = $completed / ($total - $excluded);
}
}
else {
$progress = $total - $excluded == 0 ? 1 : 0;
}
if (!$auto_authorize_enabled && $unauthorized > 0) {
$this->logger
->warning(t("Translation progress in dashboard 100% but for the connector progress = @percentage%.", [
"@percentage" => (int) ($progress * 100),
])
->render());
}
return (int) ($progress * 100);
}
}