You are here

public function SmartlingApiWrapper::createAuditLogRecord in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 src/Smartling/SmartlingApiWrapper.php \Drupal\tmgmt_smartling\Smartling\SmartlingApiWrapper::createAuditLogRecord()

Parameters

\Drupal\tmgmt\JobInterface $job:

\Drupal\tmgmt\Entity\Translator $translator:

\Drupal\Core\Session\AccountProxyInterface $current_user:

$action_type:

File

src/Smartling/SmartlingApiWrapper.php, line 371
SmartlingApiWrapper.php.

Class

SmartlingApiWrapper
Class SmartlingApiWrapper @package Drupal\tmgmt_smartling\Smartling

Namespace

Drupal\tmgmt_smartling\Smartling

Code

public function createAuditLogRecord(JobInterface $job = NULL, Translator $translator = NULL, AccountProxyInterface $current_user, $action_type) {
  try {

    // Provider settings can be grabbed from job or from translator (support
    // for job events and translator events).
    $provider_settings = [];
    $locale_mapping = [];
    $resolved_translator = NULL;
    if (!empty($job)) {
      $resolved_translator = $job
        ->getTranslator();
    }
    if (empty($resolved_translator) && !empty($translator)) {
      $resolved_translator = $translator;
    }

    // Retrieve provider settings.
    if (!empty($resolved_translator)) {
      $locale_mapping = $resolved_translator
        ->getRemoteLanguagesMappings();
      $provider_settings = $resolved_translator
        ->getSettings();

      // Do not send sensitive data.
      unset($provider_settings['project_id'], $provider_settings['user_id'], $provider_settings['token_secret'], $provider_settings['basic_auth']);
    }
    $params = new CreateRecordParameters();
    $params
      ->setActionTime(time())
      ->setActionType($action_type)
      ->setEnvId(php_uname('n'))
      ->setClientUserId($current_user
      ->id())
      ->setClientUserEmail((string) $current_user
      ->getEmail())
      ->setClientUserName((string) $current_user
      ->getAccountName())
      ->setClientData('provider_settings', $provider_settings)
      ->setClientData('locale_mappings', $locale_mapping);
    if ($job) {
      $params
        ->setFileUri((string) $job
        ->getTranslatorPlugin()
        ->getFileName($job))
        ->setClientData('tmgmt_job_id', (string) $job
        ->id())
        ->setClientData('tmgmt_job_label', (string) $job
        ->label())
        ->setClientData('tmgmt_job_translator', (string) $job
        ->getTranslatorLabel())
        ->setClientData('tmgmt_job_state', (string) $job
        ->getState())
        ->setClientData('tmgmt_job_created', (string) $job
        ->getCreatedTime())
        ->setClientData('tmgmt_job_changed', (string) $job
        ->getChangedTime())
        ->setSourceLocaleId((string) $job
        ->getSourceLangcode())
        ->setTargetLocaleIds([
        (string) $job
          ->getRemoteTargetLanguage(),
      ])
        ->setTranslationJobUid((string) $job
        ->getSetting('job_id'))
        ->setTranslationJobName((string) $job
        ->getSetting('job_name'))
        ->setTranslationJobDueDate((string) $job
        ->getSetting('due_date'))
        ->setTranslationJobAuthorize($job
        ->getSetting('authorize'))
        ->setBatchUid((string) $job
        ->getSetting('batch_uid'))
        ->setClientData('batch_execute_on_job', (string) $job
        ->getSetting('batch_execute_on_job'));
    }
    $this
      ->getApi('audit')
      ->createProjectLevelLogRecord($params);
  } catch (Exception $e) {
    $this->logger
      ->error('Smartling failed to create an audit log record.<br/>Error: @error.', [
      '@error' => $e
        ->getMessage(),
    ]);
  }
}