You are here

class LingotekCliService in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8.2 src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  2. 4.0.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  3. 3.0.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  4. 3.1.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  5. 3.2.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  6. 3.3.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  7. 3.5.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  8. 3.6.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  9. 3.7.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService
  10. 3.8.x src/Cli/LingotekCliService.php \Drupal\lingotek\Cli\LingotekCliService

Hierarchy

Expanded class hierarchy of LingotekCliService

2 files declare their use of LingotekCliService
LingotekCliServiceTest.php in tests/src/Unit/Cli/LingotekCliServiceTest.php
LingotekCommands.php in src/Cli/Commands/Drush9/LingotekCommands.php
1 string reference to 'LingotekCliService'
lingotek.services.yml in ./lingotek.services.yml
lingotek.services.yml
1 service uses LingotekCliService
lingotek.cli_service in ./lingotek.services.yml
Drupal\lingotek\Cli\LingotekCliService

File

src/Cli/LingotekCliService.php, line 11

Namespace

Drupal\lingotek\Cli
View source
class LingotekCliService {
  const COMMAND_SUCCEDED = 0;
  const COMMAND_ERROR_ENTITY_TYPE_ID = 1;
  const COMMAND_ERROR_ENTITY_NOT_FOUND = 2;
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The Lingotek content translation service.
   *
   * @var \Drupal\lingotek\LingotekContentTranslationServiceInterface
   */
  protected $translationService;

  /**
   * The language-locale mapper.
   *
   * @var \Drupal\lingotek\LanguageLocaleMapperInterface
   */
  protected $languageLocaleMapper;

  /**
   * @var \Symfony\Component\Console\Output\OutputInterface|\Drupal\lingotek\Cli\Commands\Drush8\Drush8IoWrapper
   */
  protected $output;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface|\Drupal\lingotek\Cli\Commands\Drush8\Drush8IoWrapper
   */
  protected $logger;

  /**
   * LingotekCliService constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service
   *   The Lingotek content translation service.
   * @param \Drupal\lingotek\LanguageLocaleMapperInterface $language_locale_mapper
   *   The language-locale mapper.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, LingotekContentTranslationServiceInterface $translation_service, LanguageLocaleMapperInterface $language_locale_mapper) {
    $this->entityTypeManager = $entity_type_manager;
    $this->translationService = $translation_service;
    $this->languageLocaleMapper = $language_locale_mapper;
  }
  public function setupOutput($output) {
    $this->output = $output;
  }
  public function setLogger($logger) {
    $this->logger = $logger;
  }
  public function upload($entity_type_id, $entity_id, $job_id = NULL) {
    $entity = $this
      ->getEntity($entity_type_id, $entity_id);
    if ($entity instanceof EntityInterface) {
      $document_id = $this->translationService
        ->uploadDocument($entity, $job_id);
      $this->output
        ->writeln($document_id);
      return self::COMMAND_SUCCEDED;
    }

    // Contains an error message.
    return $entity;
  }
  public function checkUpload($entity_type_id, $entity_id) {
    $entity = $this
      ->getEntity($entity_type_id, $entity_id);
    if ($entity instanceof EntityInterface) {
      $this->translationService
        ->checkSourceStatus($entity);
      $status = $this->translationService
        ->getSourceStatus($entity);
      $this->output
        ->writeln($status);
      return self::COMMAND_SUCCEDED;
    }

    // Contains an error message.
    return $entity;
  }
  public function requestTranslations($entity_type_id, $entity_id, $langcodes = [
    'all',
  ]) {
    $entity = $this
      ->getEntity($entity_type_id, $entity_id);
    if ($entity instanceof EntityInterface) {
      $result = [];
      $languages = [];
      if (in_array('all', $langcodes)) {
        $languages = $this->translationService
          ->requestTranslations($entity);
      }
      else {
        foreach ($langcodes as $langcode) {
          $locale = $this->languageLocaleMapper
            ->getLocaleForLangcode($langcode);
          if ($locale) {
            $targetAdded = $this->translationService
              ->addTarget($entity, $locale);
            if ($targetAdded) {
              $languages[] = $langcode;
            }
            else {
              $this->logger
                ->error($this
                ->t('Language %langcode could not be requested.', [
                '%langcode' => $langcode,
              ]));
            }
          }
          else {
            $this->logger
              ->error($this
              ->t('Language %langcode is not valid.', [
              '%langcode' => $langcode,
            ]));
          }
        }
      }
      foreach ($languages as $langcode) {
        $result[$langcode] = [
          'langcode' => $langcode,
        ];
      }
      return $result;
    }

    // Contains an error message.
    return $entity;
  }
  public function checkTranslationsStatuses($entity_type_id, $entity_id, $langcodes = [
    'all',
  ]) {
    $entity = $this
      ->getEntity($entity_type_id, $entity_id);
    if ($entity instanceof EntityInterface) {
      $this->translationService
        ->checkTargetStatuses($entity);
      $languages = $this->translationService
        ->getTargetStatuses($entity);
      unset($languages[$entity
        ->getUntranslated()
        ->language()
        ->getId()]);
      $result = [];
      foreach ($languages as $langcode => $status) {
        if (!in_array('all', $langcodes)) {
          if (!in_array($langcode, $langcodes)) {
            continue;
          }
        }
        $result[$langcode] = [
          'langcode' => $langcode,
          'status' => $status,
        ];
      }
      return $result;
    }

    // Contains an error message.
    return $entity;
  }
  public function downloadTranslations($entity_type_id, $entity_id, $langcodes = [
    'all',
  ]) {
    $entity = $this
      ->getEntity($entity_type_id, $entity_id);
    if ($entity instanceof EntityInterface) {
      if (in_array('all', $langcodes)) {
        $this->translationService
          ->downloadDocuments($entity);
      }
      else {
        foreach ($langcodes as $langcode) {
          $locale = $this->languageLocaleMapper
            ->getLocaleForLangcode($langcode);
          if ($locale) {
            $this->translationService
              ->downloadDocument($entity, $locale);
          }
          else {
            $this->logger
              ->error($this
              ->t('Language %langcode is not valid.', [
              '%langcode' => $langcode,
            ]));
          }
        }
      }
      return self::COMMAND_SUCCEDED;
    }

    // Contains an error message.
    return $entity;
  }
  public function getEntity($entity_type_id, $entity_id) {
    $entity_storage = NULL;
    if (!$this->entityTypeManager
      ->hasDefinition($entity_type_id) || !($entity_storage = $this->entityTypeManager
      ->getStorage($entity_type_id))) {
      $this->logger
        ->error($this
        ->t('Invalid entity type id: @entity_type_id', [
        '@entity_type_id' => $entity_type_id,
      ]));
      return self::COMMAND_ERROR_ENTITY_TYPE_ID;
    }

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $entity_storage
      ->load($entity_id);
    if (!$entity) {
      $this->logger
        ->error($this
        ->t('Entity of type @entity_type_id with id @entity_id not found.', [
        '@entity_type_id' => $entity_type_id,
        '@entity_id' => $entity_id,
      ]));
      return self::COMMAND_ERROR_ENTITY_NOT_FOUND;
    }
    return $entity;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekCliService::$entityTypeManager protected property The entity type manager.
LingotekCliService::$languageLocaleMapper protected property The language-locale mapper.
LingotekCliService::$logger protected property A logger instance.
LingotekCliService::$output protected property
LingotekCliService::$translationService protected property The Lingotek content translation service.
LingotekCliService::checkTranslationsStatuses public function
LingotekCliService::checkUpload public function
LingotekCliService::COMMAND_ERROR_ENTITY_NOT_FOUND constant
LingotekCliService::COMMAND_ERROR_ENTITY_TYPE_ID constant
LingotekCliService::COMMAND_SUCCEDED constant
LingotekCliService::downloadTranslations public function
LingotekCliService::getEntity public function
LingotekCliService::requestTranslations public function
LingotekCliService::setLogger public function
LingotekCliService::setupOutput public function
LingotekCliService::upload public function
LingotekCliService::__construct public function LingotekCliService constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.