View source
<?php
namespace Smartling\Jobs;
use Psr\Log\LoggerInterface;
use Smartling\AuthApi\AuthApiInterface;
use Smartling\BaseApiAbstract;
use Smartling\Exceptions\SmartlingApiException;
use Smartling\Jobs\Params\AddFileToJobParameters;
use Smartling\Jobs\Params\AddLocaleToJobParameters;
use Smartling\Jobs\Params\CancelJobParameters;
use Smartling\Jobs\Params\CreateJobParameters;
use Smartling\Jobs\Params\ListJobsParameters;
use Smartling\Jobs\Params\SearchJobsParameters;
use Smartling\Jobs\Params\UpdateJobParameters;
class JobsApi extends BaseApiAbstract {
const ENDPOINT_URL = 'https://api.smartling.com/jobs-api/v3/projects';
private $syncTimeOut = 15;
public function getSyncTimeOut() {
return $this->syncTimeOut;
}
public function setSyncTimeOut($syncTimeOut) {
$this->syncTimeOut = $syncTimeOut;
}
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');
}
}
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;
}
public function createJob(CreateJobParameters $parameters) {
$requestData = $this
->getDefaultRequestData('json', $parameters
->exportToArray());
return $this
->sendRequest('jobs', $requestData, self::HTTP_METHOD_POST);
}
public function updateJob($jobId, UpdateJobParameters $parameters) {
$requestData = $this
->getDefaultRequestData('json', $parameters
->exportToArray());
return $this
->sendRequest('jobs/' . $jobId, $requestData, self::HTTP_METHOD_PUT);
}
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));
}
public function listJobs(ListJobsParameters $parameters) {
$requestData = $this
->getDefaultRequestData('query', $parameters
->exportToArray());
return $this
->sendRequest('jobs', $requestData, self::HTTP_METHOD_GET);
}
public function getJob($jobId) {
$requestData = $this
->getDefaultRequestData('query', []);
return $this
->sendRequest('jobs/' . $jobId, $requestData, self::HTTP_METHOD_GET);
}
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);
}
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));
}
public function searchJobs(SearchJobsParameters $parameters) {
$requestData = $this
->getDefaultRequestData('json', $parameters
->exportToArray());
return $this
->sendRequest('jobs/search', $requestData, self::HTTP_METHOD_POST);
}
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));
}
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);
}
}