Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/CrowdinApiClient/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,29 @@ protected function _create(string $path, string $modelName, array $data)
/**
* @param string $path
* @param array $params
* @param string|null $modelName
* @param array $headers
* @return mixed
*/
protected function _delete(string $path, array $params = [])
{
protected function _delete(
string $path,
array $params = [],
?string $modelName = null,
array $headers = []
) {
$options = [];

if (!empty($params)) {
$options['params'] = $params;
}

return $this->client->apiRequest('delete', $path, null, $options);
if (!empty($headers)) {
$options['headers'] = array_merge($this->getHeaders(), $headers);
}

$decorator = $modelName ? new ResponseModelDecorator($modelName) : null;

return $this->client->apiRequest('delete', $path, $decorator, $options);
}

/**
Expand Down
27 changes: 25 additions & 2 deletions src/CrowdinApiClient/Api/BranchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CrowdinApiClient\Model\BranchClone;
use CrowdinApiClient\Model\BranchMerge;
use CrowdinApiClient\Model\BranchMergeSummary;
use CrowdinApiClient\Model\DeleteJob;
use CrowdinApiClient\ModelCollection;

/**
Expand Down Expand Up @@ -92,12 +93,34 @@ public function update(Branch $branch): ?Branch
*
* @param int $projectId
* @param int $branchId
* @param string|null $prefer
* @return mixed
*/
public function delete(int $projectId, int $branchId)
public function delete(int $projectId, int $branchId, ?string $prefer = null)
{
$path = sprintf('projects/%d/branches/%d', $projectId, $branchId);
return $this->_delete($path);

if ($prefer === null) {
return $this->_delete($path);
}

return $this->_delete($path, [], DeleteJob::class, ['prefer' => $prefer]);
}

/**
* Check Delete Branch Job Status
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.branches.jobs.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.branches.jobs.get API Documentation Enterprise
*
* @param int $projectId
* @param int $branchId
* @param string $jobIdentifier
* @return DeleteJob|null
*/
public function checkDeleteStatus(int $projectId, int $branchId, string $jobIdentifier): ?DeleteJob
{
$path = sprintf('projects/%d/branches/%d/jobs/%s', $projectId, $branchId, $jobIdentifier);
return $this->_get($path, DeleteJob::class);
}

/**
Expand Down
27 changes: 25 additions & 2 deletions src/CrowdinApiClient/Api/DirectoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CrowdinApiClient\Api;

use CrowdinApiClient\Model\DeleteJob;
use CrowdinApiClient\Model\Directory;
use CrowdinApiClient\ModelCollection;

Expand Down Expand Up @@ -89,11 +90,33 @@ public function update(Directory $directory): ?Directory
*
* @param int $projectId
* @param int $directoryId
* @param string|null $prefer
* @return mixed
*/
public function delete(int $projectId, int $directoryId)
public function delete(int $projectId, int $directoryId, ?string $prefer = null)
{
$path = sprintf('projects/%d/directories/%d', $projectId, $directoryId);
return $this->_delete($path);

if ($prefer === null) {
return $this->_delete($path);
}

return $this->_delete($path, [], DeleteJob::class, ['prefer' => $prefer]);
}

/**
* Check Delete Directory Job Status
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.directories.jobs.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.directories.jobs.get API Documentation Enterprise
*
* @param int $projectId
* @param int $directoryId
* @param string $jobIdentifier
* @return DeleteJob|null
*/
public function checkDeleteStatus(int $projectId, int $directoryId, string $jobIdentifier): ?DeleteJob
{
$path = sprintf('projects/%d/directories/%d/jobs/%s', $projectId, $directoryId, $jobIdentifier);
return $this->_get($path, DeleteJob::class);
}
}
29 changes: 26 additions & 3 deletions src/CrowdinApiClient/Api/FileApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CrowdinApiClient\Api;

use CrowdinApiClient\Model\DeleteJob;
use CrowdinApiClient\Model\DownloadFile;
use CrowdinApiClient\Model\DownloadFilePreview;
use CrowdinApiClient\Model\File;
Expand Down Expand Up @@ -146,12 +147,34 @@ public function edit(File $file): ?File
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.files.delete API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.files.delete API Documentation Enterprise
*
* @return null
* @param string|null $prefer
* @return mixed
*/
public function delete(int $projectId, int $fileId)
public function delete(int $projectId, int $fileId, ?string $prefer = null)
{
$path = sprintf('projects/%d/files/%d', $projectId, $fileId);
return $this->_delete($path);

if ($prefer === null) {
return $this->_delete($path);
}

return $this->_delete($path, [], DeleteJob::class, ['prefer' => $prefer]);
}

/**
* Check Delete File Job Status
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.files.jobs.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.files.jobs.get API Documentation Enterprise
*
* @param int $projectId
* @param int $fileId
* @param string $jobIdentifier
* @return DeleteJob|null
*/
public function checkDeleteStatus(int $projectId, int $fileId, string $jobIdentifier): ?DeleteJob
{
$path = sprintf('projects/%d/files/%d/jobs/%s', $projectId, $fileId, $jobIdentifier);
return $this->_get($path, DeleteJob::class);
}

/**
Expand Down
141 changes: 141 additions & 0 deletions src/CrowdinApiClient/Model/DeleteJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace CrowdinApiClient\Model;

/**
* @package Crowdin\Model
*/
class DeleteJob extends BaseModel
{
/**
* @var string
*/
protected $identifier;

/**
* @var string
*/
protected $status;

/**
* @var integer
*/
protected $progress;

/**
* @var array
*/
protected $attributes;

/**
* @var string
*/
protected $createdAt;

/**
* @var string
*/
protected $updatedAt;

/**
* @var string|null
*/
protected $startedAt;

/**
* @var string|null
*/
protected $finishedAt;

/**
* @var array|null
*/
protected $error;

public function __construct(array $data = [])
{
parent::__construct($data);

$this->identifier = (string)$this->getDataProperty('identifier');
$this->status = (string)$this->getDataProperty('status');
$this->progress = (int)$this->getDataProperty('progress');
$this->attributes = (array)$this->getDataProperty('attributes');
$this->createdAt = (string)$this->getDataProperty('createdAt');
$this->updatedAt = (string)$this->getDataProperty('updatedAt');
$this->startedAt = $this->getDataProperty('startedAt');
$this->finishedAt = $this->getDataProperty('finishedAt');
$this->error = $this->getDataProperty('error');
}

/**
* @return string
*/
public function getIdentifier(): string
{
return $this->identifier;
}

/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}

/**
* @return int
*/
public function getProgress(): int
{
return $this->progress;
}

/**
* @return array
*/
public function getAttributes(): array
{
return $this->attributes;
}

/**
* @return string
*/
public function getCreatedAt(): string
{
return $this->createdAt;
}

/**
* @return string
*/
public function getUpdatedAt(): string
{
return $this->updatedAt;
}

/**
* @return string|null
*/
public function getStartedAt(): ?string
{
return $this->startedAt;
}

/**
* @return string|null
*/
public function getFinishedAt(): ?string
{
return $this->finishedAt;
}

/**
* @return array|null
*/
public function getError(): ?array
{
return $this->error;
}
}
66 changes: 66 additions & 0 deletions tests/CrowdinApiClient/Api/BranchApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CrowdinApiClient\Model\BranchClone;
use CrowdinApiClient\Model\BranchMerge;
use CrowdinApiClient\Model\BranchMergeSummary;
use CrowdinApiClient\Model\DeleteJob;
use CrowdinApiClient\ModelCollection;

class BranchApiTest extends AbstractTestApi
Expand Down Expand Up @@ -133,6 +134,71 @@ public function testDelete()
$this->crowdin->branch->delete(2, 34);
}

public function testDeleteAsync(): void
{
$this->mockRequest([
'path' => '/projects/2/branches/34',
'method' => 'delete',
'headers' => [
'Authorization' => 'Bearer access_token',
'content-type' => 'application/json',
'prefer' => 'respond-async',
],
'response' => json_encode([
'data' => [
'identifier' => '50fb3506-4127-4ba8-8296-f97dc7e3e0c3',
'status' => 'created',
'progress' => 0,
'attributes' => [
'branchId' => 34,
],
'createdAt' => '2019-09-23T11:26:54+00:00',
'updatedAt' => '2019-09-23T11:26:54+00:00',
'startedAt' => null,
'finishedAt' => null,
],
]),
]);

$deleteJob = $this->crowdin->branch->delete(2, 34, 'respond-async');

$this->assertInstanceOf(DeleteJob::class, $deleteJob);
$this->assertEquals('50fb3506-4127-4ba8-8296-f97dc7e3e0c3', $deleteJob->getIdentifier());
$this->assertEquals('created', $deleteJob->getStatus());
}

public function testCheckDeleteStatus(): void
{
$this->mockRequestGet(
'/projects/2/branches/34/jobs/50fb3506-4127-4ba8-8296-f97dc7e3e0c3',
json_encode([
'data' => [
'identifier' => '50fb3506-4127-4ba8-8296-f97dc7e3e0c3',
'status' => 'finished',
'progress' => 100,
'attributes' => [
'branchId' => 34,
],
'createdAt' => '2019-09-23T11:26:54+00:00',
'updatedAt' => '2019-09-23T11:26:56+00:00',
'startedAt' => '2019-09-23T11:26:55+00:00',
'finishedAt' => '2019-09-23T11:26:56+00:00',
],
])
);

$deleteJob = $this->crowdin->branch->checkDeleteStatus(
2,
34,
'50fb3506-4127-4ba8-8296-f97dc7e3e0c3'
);

$this->assertInstanceOf(DeleteJob::class, $deleteJob);
$this->assertEquals('finished', $deleteJob->getStatus());
$this->assertEquals(100, $deleteJob->getProgress());
$this->assertEquals(['branchId' => 34], $deleteJob->getAttributes());
}

public function testCloneBranch(): void
{
$params = [
Expand Down
Loading
Loading