You are here

public function TranslationProgressCalculator::calculate in TMGMT Translator Smartling 8.3

Calculates translation progress.

Parameters

array $data:

bool $auto_authorize_enabled:

Return value

int

Throws

\Exception

File

src/Smartling/TranslationProgressCalculator.php, line 73

Class

TranslationProgressCalculator
Class TranslationProgressCalculator.

Namespace

Drupal\tmgmt_smartling\Smartling

Code

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 at least 1 string is authorized or completed then calculate progress
  // based on this information.
  if ($authorized + $completed > 0) {

    // Use different formula depending on configured behavior.
    if ($auto_authorize_enabled) {

      // Disregard unauthorized strings in progress.
      // This is how "Files" tab in dashboard calculates it.
      $progress = $completed / ($authorized + $completed);
    }
    else {

      // Progress takes into account unauthorized + authorized + completed
      // strings. So if file has at least one unauthorized string
      // then progress will be always < 100%.
      $progress = $completed / ($total - $excluded);
    }
  }
  else {

    // If nothing is authorized then check special case when everything was
    // excluded.
    $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);
}