View source
<?php
namespace Smartling\Batch;
use Psr\Log\LoggerInterface;
use Smartling\AuthApi\AuthApiInterface;
use Smartling\BaseApiAbstract;
use Smartling\Batch\Params\CreateBatchParameters;
use Smartling\Exceptions\SmartlingApiException;
use Smartling\File\Params\UploadFileParameters;
class BatchApi extends BaseApiAbstract {
const ACTION_EXECUTE = 'execute';
const ENDPOINT_URL = 'https://api.smartling.com/jobs-batch-api/v1/projects';
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;
}
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;
}
public function createBatch(CreateBatchParameters $parameters) {
$requestData = $this
->getDefaultRequestData('json', $parameters
->exportToArray());
return $this
->sendRequest('batches', $requestData, self::HTTP_METHOD_POST);
}
public function uploadBatchFile($realPath, $fileName, $fileType, $batchUid, UploadFileParameters $parameters = null) {
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);
}
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);
}
public function getBatchStatus($batchUid) {
$endpoint = vsprintf('batches/%s', [
$batchUid,
]);
$requestData = $this
->getDefaultRequestData('query', []);
return $this
->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET);
}
public function listBatches() {
$requestData = $this
->getDefaultRequestData('query', []);
return $this
->sendRequest('batches', $requestData, self::HTTP_METHOD_GET);
}
}