You are here

class TranslationProgressCalculator in TMGMT Translator Smartling 8.3

Class TranslationProgressCalculator.

Hierarchy

Expanded class hierarchy of TranslationProgressCalculator

2 files declare their use of TranslationProgressCalculator
SmartlingTranslator.php in src/Plugin/tmgmt/Translator/SmartlingTranslator.php
Contains \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator.
TranslationProgressCalculatorTest.php in tests/src/Kernel/TranslationProgressCalculatorTest.php
1 string reference to 'TranslationProgressCalculator'
tmgmt_smartling.services.yml in ./tmgmt_smartling.services.yml
tmgmt_smartling.services.yml
1 service uses TranslationProgressCalculator
tmgmt_smartling.translation_progress_calculator in ./tmgmt_smartling.services.yml
\Drupal\tmgmt_smartling\Smartling\TranslationProgressCalculator

File

src/Smartling/TranslationProgressCalculator.php, line 10

Namespace

Drupal\tmgmt_smartling\Smartling
View source
class TranslationProgressCalculator {

  /**
   * @var string
   */
  private $totalKey = "totalStringCount";

  /**
   * @var string
   */
  private $authorizedKey = "authorizedStringCount";

  /**
   * @var string
   */
  private $completedKey = "completedStringCount";

  /**
   * @var string
   */
  private $excludedKey = "excludedStringCount";

  /**
   * @var \Psr\Log\LoggerInterface
   */
  private $logger;
  public function __construct(LoggerInterface $logger) {
    $this->logger = $logger;
  }

  /**
   * Validates input data.
   *
   * @param array $data
   *
   * @return bool
   */
  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;
  }

  /**
   * Calculates translation progress.
   *
   * @param array $data
   * @param bool $auto_authorize_enabled
   *
   * @return int
   *
   * @throws \Exception
   */
  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);
  }

}

Members