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
79 changes: 78 additions & 1 deletion src/CrowdinApiClient/Api/ApplicationApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace CrowdinApiClient\Api;

use CrowdinApiClient\Model\ApplicationData;
use CrowdinApiClient\Model\ApplicationInstallation;
use CrowdinApiClient\ModelCollection;

/**
* Use API to manage custom application data.
Expand Down Expand Up @@ -76,14 +78,89 @@ public function deleteApplicationData(string $applicationIdentifier, string $pat
return $this->_delete($url);
}

/**
* List Application Installations
* @link https://developer.crowdin.com/api/v2/#operation/api.applications.installations.getMany API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.installations.getMany API Documentation Enterprise
*
* @param array $params
* integer $params[limit]<br>
* integer $params[offset]<br>
* integer $params[installedBy]<br>
* string $params[orderBy]
* @return ModelCollection
*/
public function listInstallations(array $params = []): ModelCollection
{
return $this->_list('applications/installations', ApplicationInstallation::class, $params);
}

/**
* Install Application
* @link https://developer.crowdin.com/api/v2/#operation/api.applications.installations.post API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.installations.post API Documentation Enterprise
*
* @param array $data
* string $data[url] required<br>
* array $data[permissions]<br>
* array $data[modules]<br>
* bool $data[assignAgent]
* @return ApplicationInstallation|null
*/
public function installApplication(array $data): ?ApplicationInstallation
{
return $this->_create('applications/installations', ApplicationInstallation::class, $data);
}

/**
* Get Application Installation
* @link https://developer.crowdin.com/api/v2/#operation/api.applications.installations.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.installations.get API Documentation Enterprise
*
* @param string $identifier
* @return ApplicationInstallation|null
*/
public function getInstallation(string $identifier): ?ApplicationInstallation
{
return $this->_get('applications/installations/' . $identifier, ApplicationInstallation::class);
}

/**
* Delete Application Installation
* @link https://developer.crowdin.com/api/v2/#operation/api.applications.installations.delete API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.installations.delete API Documentation Enterprise
*
* @param string $identifier
* @param bool $force
* @return mixed
*/
public function deleteInstallation(string $identifier, bool $force = false)
{
$params = $force ? ['force' => true] : [];
return $this->_delete('applications/installations/' . $identifier, $params);
}

/**
* Edit Application Installation
* @link https://developer.crowdin.com/api/v2/#operation/api.applications.installations.patch API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.installations.patch API Documentation Enterprise
*
* @param ApplicationInstallation $installation
* @return ApplicationInstallation|null
*/
public function updateInstallation(ApplicationInstallation $installation): ?ApplicationInstallation
{
return $this->_update('applications/installations/' . $installation->getIdentifier(), $installation);
}

/**
* Edit Application Data
* @link https://developer.crowdin.com/api/v2/#operation/api.applications.api.patch API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.api.patch API Documentation Enterprise
*
* @param string $applicationIdentifier
* @param string $path
* @param array $data
* @param array $data Application-specific key-value payload (free-form, not RFC 6902 patch operations)
* @return ApplicationData|null
*/
Comment thread
innomaxx marked this conversation as resolved.
public function editApplicationData(string $applicationIdentifier, string $path, array $data): ?ApplicationData
Expand Down
165 changes: 165 additions & 0 deletions src/CrowdinApiClient/Model/ApplicationInstallation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

declare(strict_types=1);

namespace CrowdinApiClient\Model;

class ApplicationInstallation extends BaseModel
{
/** @var string */
protected $identifier;

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

/** @var array|null */
protected $installedBy = null;

/** @var string|null */
protected $description = null;

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

/** @var array|null */
protected $agent = null;

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

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

/** @var string|null */
protected $createdAt = null;

/** @var array */
protected $modules = [];

/** @var array */
protected $scopes = [];

/** @var array */
protected $permissions = [];

/** @var array */
protected $defaultPermissions = [];

/** @var bool */
protected $limitReached;

/** @var bool */
protected $stringBasedAvailable;

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

$this->identifier = (string)$this->getDataProperty('identifier');
$this->name = (string)$this->getDataProperty('name');
$this->installedBy = $this->getDataProperty('installedBy');
$this->description = $this->getDataProperty('description');
$this->logo = (string)$this->getDataProperty('logo');
$this->agent = $this->getDataProperty('agent');
$this->baseUrl = (string)$this->getDataProperty('baseUrl');
$this->manifestUrl = (string)$this->getDataProperty('manifestUrl');
$this->createdAt = $this->getDataProperty('createdAt');
$this->modules = (array)$this->getDataProperty('modules');
$this->scopes = (array)$this->getDataProperty('scopes');
$this->permissions = (array)$this->getDataProperty('permissions');
$this->defaultPermissions = (array)$this->getDataProperty('defaultPermissions');
$this->limitReached = (bool)$this->getDataProperty('limitReached');
$this->stringBasedAvailable = (bool)$this->getDataProperty('stringBasedAvailable');
}

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

public function getName(): string
{
return $this->name;
}

/**
* @return array|null
*/
public function getInstalledBy(): ?array
{
return $this->installedBy;
}

public function getDescription(): ?string
{
return $this->description;
}

public function getLogo(): string
{
return $this->logo;
}

/**
* @return array|null
*/
public function getAgent(): ?array
{
return $this->agent;
}

public function getBaseUrl(): string
{
return $this->baseUrl;
}

public function getManifestUrl(): string
{
return $this->manifestUrl;
}

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

public function getModules(): array
{
return $this->modules;
}

public function getScopes(): array
{
return $this->scopes;
}

public function getPermissions(): array
{
return $this->permissions;
}

public function getDefaultPermissions(): array
{
return $this->defaultPermissions;
}

public function isLimitReached(): bool
{
return $this->limitReached;
}

public function isStringBasedAvailable(): bool
{
return $this->stringBasedAvailable;
}

public function setPermissions(array $permissions): void
{
$this->permissions = $permissions;
}

public function setModules(array $modules): void
{
$this->modules = $modules;
}
}
105 changes: 105 additions & 0 deletions tests/CrowdinApiClient/Api/ApplicationApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace CrowdinApiClient\Tests\Api;

use CrowdinApiClient\Model\ApplicationData;
use CrowdinApiClient\Model\ApplicationInstallation;
use CrowdinApiClient\ModelCollection;

class ApplicationApiTest extends AbstractTestApi
{
Expand All @@ -26,6 +28,109 @@ protected function getResponseJson(): string
]);
}

protected function getInstallationData(): array
{
return [
'identifier' => 'my-app',
'name' => 'My Application',
'installedBy' => ['id' => 1, 'username' => 'admin'],
'description' => 'App description',
'logo' => '/resources/images/logo.png',
'agent' => null,
'baseUrl' => 'https://localhost.dev',
'manifestUrl' => 'https://localhost.dev/manifest.json',
'createdAt' => '2023-11-29T10:00:00+00:00',
'modules' => [],
'scopes' => ['application'],
'permissions' => ['user' => ['value' => 'all']],
'defaultPermissions' => [],
'limitReached' => false,
'stringBasedAvailable' => false,
];
}

protected function getInstallationResponseJson(): string
{
return json_encode(['data' => $this->getInstallationData()]);
}

public function testListInstallations(): void
{
$this->mockRequest([
'path' => '/applications/installations',
'method' => 'get',
'response' => json_encode([
'data' => [
['data' => $this->getInstallationData()],
],
'pagination' => [['offset' => 0, 'limit' => 25]],
]),
]);

$list = $this->crowdin->application->listInstallations();

$this->assertInstanceOf(ModelCollection::class, $list);
$this->assertCount(1, $list);
$this->assertInstanceOf(ApplicationInstallation::class, $list[0]);
$this->assertEquals('my-app', $list[0]->getIdentifier());
$this->assertEquals('My Application', $list[0]->getName());
}

public function testInstallApplication(): void
{
$this->mockRequest([
'path' => '/applications/installations',
'method' => 'post',
'body' => json_encode(['url' => 'https://localhost.dev/manifest.json']),
'response' => $this->getInstallationResponseJson(),
]);

$installation = $this->crowdin->application->installApplication([
'url' => 'https://localhost.dev/manifest.json',
]);

$this->assertInstanceOf(ApplicationInstallation::class, $installation);
$this->assertEquals('my-app', $installation->getIdentifier());
$this->assertEquals('My Application', $installation->getName());
}

public function testGetInstallation(): void
{
$this->mockRequestGet(
'/applications/installations/my-app',
$this->getInstallationResponseJson()
);

$installation = $this->crowdin->application->getInstallation('my-app');

$this->assertInstanceOf(ApplicationInstallation::class, $installation);
$this->assertEquals('my-app', $installation->getIdentifier());
$this->assertEquals('My Application', $installation->getName());
}

public function testDeleteInstallation(): void
{
$this->mockRequestDelete('/applications/installations/my-app');

$this->crowdin->application->deleteInstallation('my-app');
}

public function testUpdateInstallation(): void
{
$this->mockRequestPatch(
'/applications/installations/my-app',
$this->getInstallationResponseJson()
);

$installation = new ApplicationInstallation($this->getInstallationData());
$installation->setPermissions(['user' => ['value' => 'managers']]);

$result = $this->crowdin->application->updateInstallation($installation);

$this->assertInstanceOf(ApplicationInstallation::class, $result);
$this->assertEquals('my-app', $result->getIdentifier());
}

public function testGetApplicationData(): void
{
$this->mockRequestGet(
Expand Down
Loading
Loading