You are here

class JobsApi in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8.3 vendor/smartling/api-sdk-php/src/Jobs/JobsApi.php \Smartling\Jobs\JobsApi

Class JobsApi

@package Smartling\Jobs

Hierarchy

Expanded class hierarchy of JobsApi

3 files declare their use of JobsApi
JobsApiFunctionalTest.php in vendor/smartling/api-sdk-php/tests/functional/JobsApiFunctionalTest.php
JobsApiTest.php in vendor/smartling/api-sdk-php/tests/unit/JobsApiTest.php
SmartlingApiFactory.php in src/Smartling/SmartlingApiFactory.php
SmartlingApiFactory.php.

File

vendor/smartling/api-sdk-php/src/Jobs/JobsApi.php, line 22

Namespace

Smartling\Jobs
View source
class JobsApi extends BaseApiAbstract {
  const ENDPOINT_URL = 'https://api.smartling.com/jobs-api/v3/projects';

  /**
   * Timeout for sync requests in seconds.
   *
   * @var int
   */
  private $syncTimeOut = 15;

  /**
   * @return int
   */
  public function getSyncTimeOut() {
    return $this->syncTimeOut;
  }

  /**
   * @param int $syncTimeOut
   */
  public function setSyncTimeOut($syncTimeOut) {
    $this->syncTimeOut = $syncTimeOut;
  }

  /**
   * Makes async operation sync.
   *
   * @param mixed $response
   * @throws SmartlingApiException
   */
  private function wait($response) {
    if (is_array($response) && !empty($response['url'])) {
      $explodedUrl = explode('/', $response['url']);
      $arrayLength = count($explodedUrl);
      $processId = $explodedUrl[$arrayLength - 1];
      $jobId = $explodedUrl[$arrayLength - 3];
      $start_time = time();
      do {
        $delta = time() - $start_time;
        if ($delta > $this
          ->getSyncTimeOut()) {
          throw new SmartlingApiException(vsprintf('No response received after %s seconds.', [
            $delta,
          ]));
        }
        sleep(1);
        $result = $this
          ->checkAsynchronousProcessingStatus($jobId, $processId);
      } while ($result['processState'] != 'COMPLETED');
    }
  }

  /**
   * Instantiates Jobs API object.
   *
   * @param AuthApiInterface $authProvider
   * @param string $projectId
   * @param LoggerInterface $logger
   *
   * @return JobsApi
   */
  public static function create(AuthApiInterface $authProvider, $projectId, $logger = null) {
    $client = self::initializeHttpClient(self::ENDPOINT_URL);
    $instance = new self($projectId, $client, $logger, self::ENDPOINT_URL);
    $instance
      ->setAuth($authProvider);
    return $instance;
  }

  /**
   * Creates a job.
   *
   * @param CreateJobParameters $parameters
   * @return array
   * @throws SmartlingApiException
   */
  public function createJob(CreateJobParameters $parameters) {
    $requestData = $this
      ->getDefaultRequestData('json', $parameters
      ->exportToArray());
    return $this
      ->sendRequest('jobs', $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Updates a job.
   *
   * @param string $jobId
   * @param UpdateJobParameters $parameters
   * @return array
   * @throws SmartlingApiException
   */
  public function updateJob($jobId, UpdateJobParameters $parameters) {
    $requestData = $this
      ->getDefaultRequestData('json', $parameters
      ->exportToArray());
    return $this
      ->sendRequest('jobs/' . $jobId, $requestData, self::HTTP_METHOD_PUT);
  }

  /**
   * Cancels a job synchronously.
   *
   * @param string $jobId
   * @param CancelJobParameters $parameters
   * @throws SmartlingApiException
   */
  public function cancelJobSync($jobId, CancelJobParameters $parameters) {
    $endpoint = vsprintf('jobs/%s/cancel', [
      $jobId,
    ]);
    $requestData = $this
      ->getDefaultRequestData('json', $parameters
      ->exportToArray());
    $this
      ->wait($this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST));
  }

  /**
   * Returns a list of jobs.
   *
   * @param ListJobsParameters $parameters
   * @return array
   * @throws SmartlingApiException
   */
  public function listJobs(ListJobsParameters $parameters) {
    $requestData = $this
      ->getDefaultRequestData('query', $parameters
      ->exportToArray());
    return $this
      ->sendRequest('jobs', $requestData, self::HTTP_METHOD_GET);
  }

  /**
   * Returns a job.
   *
   * @param string $jobId
   * @return array
   * @throws SmartlingApiException
   */
  public function getJob($jobId) {
    $requestData = $this
      ->getDefaultRequestData('query', []);
    return $this
      ->sendRequest('jobs/' . $jobId, $requestData, self::HTTP_METHOD_GET);
  }

  /**
   * Authorizes a job.
   *
   * @param $jobId
   * @throws SmartlingApiException
   */
  public function authorizeJob($jobId) {
    $endpoint = vsprintf('jobs/%s/authorize', [
      $jobId,
    ]);
    $requestData = $this
      ->getDefaultRequestData('json', new \stdClass());
    $requestData['headers']['Content-Type'] = 'application/json';
    $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Adds file to a job synchronously.
   *
   * @param $jobId
   * @param AddFileToJobParameters $parameters
   * @throws SmartlingApiException
   */
  public function addFileToJobSync($jobId, AddFileToJobParameters $parameters) {
    $endpoint = vsprintf('jobs/%s/file/add', [
      $jobId,
    ]);
    $requestData = $this
      ->getDefaultRequestData('json', $parameters
      ->exportToArray());
    $this
      ->wait($this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST));
  }

  /**
   * Search/Find Job(s), based on different query criteria passed in.
   *
   * @param SearchJobsParameters $parameters
   *
   * @return array
   * @throws SmartlingApiException
   */
  public function searchJobs(SearchJobsParameters $parameters) {
    $requestData = $this
      ->getDefaultRequestData('json', $parameters
      ->exportToArray());
    return $this
      ->sendRequest('jobs/search', $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Adds locale to a job synchronously.
   *
   * @param string $jobId
   * @param string $localeId
   * @param AddLocaleToJobParameters $parameters
   * @throws SmartlingApiException
   */
  public function addLocaleToJobSync($jobId, $localeId, AddLocaleToJobParameters $parameters) {
    $endpoint = vsprintf('jobs/%s/locales/%s', [
      $jobId,
      $localeId,
    ]);
    $requestData = $this
      ->getDefaultRequestData('json', $parameters
      ->exportToArray());
    $this
      ->wait($this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST));
  }

  /**
   * Checks status of async process.
   *
   * @param string $jobId
   * @param string $processId
   * @return array
   * @throws SmartlingApiException
   */
  public function checkAsynchronousProcessingStatus($jobId, $processId) {
    $endpoint = vsprintf('jobs/%s/processes/%s', [
      $jobId,
      $processId,
    ]);
    $requestData = $this
      ->getDefaultRequestData('query', []);
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseApiAbstract::$auth private property
BaseApiAbstract::$baseUrl private property Smartling API base url.
BaseApiAbstract::$currentClientId private static property
BaseApiAbstract::$currentClientUserAgentExtension private static property
BaseApiAbstract::$currentClientVersion private static property
BaseApiAbstract::$httpClient private property Http Client abstraction.
BaseApiAbstract::$logger private property Logger.
BaseApiAbstract::$projectId private property Project Id in Smartling dashboard
BaseApiAbstract::checkAuthenticationError private function
BaseApiAbstract::CLIENT_LIB_ID_SDK constant
BaseApiAbstract::CLIENT_LIB_ID_VERSION constant
BaseApiAbstract::CLIENT_USER_AGENT_EXTENSION constant
BaseApiAbstract::getAuth protected function
BaseApiAbstract::getBaseUrl protected function
BaseApiAbstract::getCurrentClientId public static function
BaseApiAbstract::getCurrentClientUserAgentExtension public static function
BaseApiAbstract::getCurrentClientVersion public static function
BaseApiAbstract::getDefaultRequestData protected function 1
BaseApiAbstract::getHttpClient protected function
BaseApiAbstract::getLogger protected function
BaseApiAbstract::getProjectId protected function
BaseApiAbstract::HTTP_METHOD_DELETE constant
BaseApiAbstract::HTTP_METHOD_GET constant
BaseApiAbstract::HTTP_METHOD_POST constant
BaseApiAbstract::HTTP_METHOD_PUT constant
BaseApiAbstract::initializeHttpClient protected static function
BaseApiAbstract::normalizeUri private function
BaseApiAbstract::PATTERN_DATE_TIME_ISO_8601 constant PHP equivalent to 'YYYY-MM-DDThh:mm:ssZ'
BaseApiAbstract::processBodyOptions protected function 3
BaseApiAbstract::processError private function
BaseApiAbstract::processErrors private function
BaseApiAbstract::readFile protected function OOP wrapper for fopen() function.
BaseApiAbstract::sendRequest protected function
BaseApiAbstract::setAuth protected function
BaseApiAbstract::setBaseUrl protected function
BaseApiAbstract::setCurrentClientId public static function
BaseApiAbstract::setCurrentClientUserAgentExtension public static function
BaseApiAbstract::setCurrentClientVersion public static function
BaseApiAbstract::setHttpClient protected function
BaseApiAbstract::setLogger protected function
BaseApiAbstract::setProjectId protected function
BaseApiAbstract::__construct public function BaseApiAbstract constructor. 3
JobsApi::$syncTimeOut private property Timeout for sync requests in seconds.
JobsApi::addFileToJobSync public function Adds file to a job synchronously.
JobsApi::addLocaleToJobSync public function Adds locale to a job synchronously.
JobsApi::authorizeJob public function Authorizes a job.
JobsApi::cancelJobSync public function Cancels a job synchronously.
JobsApi::checkAsynchronousProcessingStatus public function Checks status of async process.
JobsApi::create public static function Instantiates Jobs API object.
JobsApi::createJob public function Creates a job.
JobsApi::ENDPOINT_URL constant
JobsApi::getJob public function Returns a job.
JobsApi::getSyncTimeOut public function
JobsApi::listJobs public function Returns a list of jobs.
JobsApi::searchJobs public function Search/Find Job(s), based on different query criteria passed in.
JobsApi::setSyncTimeOut public function
JobsApi::updateJob public function Updates a job.
JobsApi::wait private function Makes async operation sync.