You are here

class BatchApi in TMGMT Translator Smartling 8.4

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

Class JobsFacadeApi

@package Smartling\Batch

Hierarchy

Expanded class hierarchy of BatchApi

4 files declare their use of BatchApi
batch-example.php in vendor/smartling/api-sdk-php/examples/batch-example.php
BatchApiTest.php in vendor/smartling/api-sdk-php/tests/unit/BatchApiTest.php
SmartlingApiFactory.php in src/Smartling/SmartlingApiFactory.php
SmartlingApiFactory.php.
SmartlingTestBase.php in tests/src/Kernel/SmartlingTestBase.php

File

vendor/smartling/api-sdk-php/src/Batch/BatchApi.php, line 17

Namespace

Smartling\Batch
View source
class BatchApi extends BaseApiAbstract {
  const ACTION_EXECUTE = 'execute';
  const ENDPOINT_URL = 'https://api.smartling.com/jobs-batch-api/v1/projects';

  /**
   * Instantiates Jobs Facade API object.
   *
   * @param AuthApiInterface $authProvider
   * @param string $projectId
   * @param LoggerInterface $logger
   *
   * @return BatchApi
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  protected function processBodyOptions($requestData = []) {
    $opts = parent::processBodyOptions($requestData);
    $key = 'file';
    if (!empty($opts['multipart'])) {
      foreach ($opts['multipart'] as &$data) {
        if ($data['name'] == $key) {
          $data['contents'] = $this
            ->readFile($data['contents']);
        }
      }
    }
    return $opts;
  }

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

  /**
   * Uploads file.
   *
   * @param $realPath
   * @param $fileName
   * @param $fileType
   * @param $batchUid
   * @param UploadFileParameters $parameters
   *
   * @return bool
   *
   * @throws SmartlingApiException
   */
  public function uploadBatchFile($realPath, $fileName, $fileType, $batchUid, UploadFileParameters $parameters = null) {

    // @TODO: let's pass file, fileUri and fileType in UploadFileParameters.
    // In this case we could get rid of passing these variables into this
    // method. But this approach requires changes in UploadFileParameters
    // class + changes in FileApi::uploadFile() method.
    if (is_null($parameters)) {
      $parameters = new UploadFileParameters();
    }
    $parameters = $parameters
      ->exportToArray();
    $parameters['file'] = $realPath;
    $parameters['fileUri'] = $fileName;
    $parameters['fileType'] = $fileType;
    $endpoint = vsprintf('batches/%s/file', [
      $batchUid,
    ]);
    $requestData = $this
      ->getDefaultRequestData('multipart', $parameters);
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Execute batch.
   *
   * @param $batchUid
   *
   * @return bool
   *
   * @throws SmartlingApiException
   */
  public function executeBatch($batchUid) {
    $endpoint = vsprintf('batches/%s', [
      $batchUid,
    ]);
    $requestData = $this
      ->getDefaultRequestData('json', [
      'action' => self::ACTION_EXECUTE,
    ]);
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_POST);
  }

  /**
   * Returns batch status.
   *
   * @param $batchUid
   *
   * @return array
   *
   * @throws SmartlingApiException
   */
  public function getBatchStatus($batchUid) {
    $endpoint = vsprintf('batches/%s', [
      $batchUid,
    ]);
    $requestData = $this
      ->getDefaultRequestData('query', []);
    return $this
      ->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET);
  }

  /**
   * Returns list of batches.
   *
   * @return array
   *
   * @throws SmartlingApiException
   */
  public function listBatches() {
    $requestData = $this
      ->getDefaultRequestData('query', []);
    return $this
      ->sendRequest('batches', $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::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
BatchApi::ACTION_EXECUTE constant
BatchApi::create public static function Instantiates Jobs Facade API object.
BatchApi::createBatch public function Creates a batch.
BatchApi::ENDPOINT_URL constant
BatchApi::executeBatch public function Execute batch.
BatchApi::getBatchStatus public function Returns batch status.
BatchApi::listBatches public function Returns list of batches.
BatchApi::processBodyOptions protected function Overrides BaseApiAbstract::processBodyOptions
BatchApi::uploadBatchFile public function Uploads file.