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
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,94 @@ $packages = $client->packages()->all($filters);
```
Returns an array of packages.

##### Show a package
```php
$package = $client->packages()->show('acme-website/package');
```
Returns the package.

##### Create a vcs package
```php
$job = $client->packages()->createVcsPackage('https://github.com/acme-website/package');
```
Returns a new job.

##### Create a vcs package with credentials
```php
$credentialId = 42;
$job = $client->packages()->createVcsPackage('https://github.com/acme-website/package', $credentialId);
```
Returns a new job.

##### Create a custom package
```php
$packageDefinition = '{...}'
$job = $client->packages()->createCustomPackage($packageDefinition);
```
Returns a new job.

##### Create a custom package with credentials
```php
$packageDefinition = '{...}'
$credentialId = 42;
$job = $client->packages()->createCustomPackage($packageDefinition, $credentialId);
```
Returns a new job.

##### Update a vcs package
```php
$job = $client->packages()->updateVcsPackage('acme-website/package', 'https://github.com/acme-website/package');
```
Returns a new job.

##### Update a custom package
```php
$packageDefinition = '{...}'
$job = $client->packages()->updateCustomPackage('acme-website/package', $packageDefinition);
```
Returns a new job.

##### Delete a package
```php
$client->packages()->remove('acme-website/package');
```

#### Credential

##### List an organization's credentials
```php
$packages = $client->credentials()->all();
```
Returns an array of credentials.

##### Show a credential
```php
$credentialId = 42;
$credential = $client->credentials()->show($credentialId);
```
Returns the credential.

##### Create a credential
```php
$type = \PrivatePackagist\ApiClient\Api\Credentials::TYPE_HTTP_BASIC;
$credential = $client->credentials()->create('ACME http auth', $type, 'acme-website.com', 'username', 'password');
```
Returns the new credential.

##### Update a credential
```php
$credentialId = 42;
$type = \PrivatePackagist\ApiClient\Api\Credentials::TYPE_HTTP_BASIC;
$credential = $client->credentials()->create($credentialId, $type, 'username', 'password');
```
Returns the updated credential.

##### Delete a credential
```php
$credentialId = 42;
$client->credentials()->remove($credentialId);
```

#### Job

##### Show a job
Expand Down
20 changes: 20 additions & 0 deletions src/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ protected function post($path, array $parameters = [], array $headers = [])
return $this->responseMediator->getContent($response);
}

/**
* @param string $path
* @param array $parameters
* @param array $headers
* @return array|string
*/
protected function put($path, array $parameters = [], array $headers = [])
{
$response = $this->client->getHttpClient()->put(
$path,
array_merge($headers, [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]),
$this->createJsonBody($parameters)
);

return $this->responseMediator->getContent($response);
}

/**
* @param string $path
* @param array $parameters
Expand Down
48 changes: 48 additions & 0 deletions src/Api/Credentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace PrivatePackagist\ApiClient\Api;

class Credentials extends AbstractApi
{
const TYPE_BITBUCKET_APP_PW = 'bitbucket-app-pw';
const TYPE_GITHUB_OAUTH = 'github-oauth';
const TYPE_GITLAB_TOKEN = 'gitlab-token';
const TYPE_HTTP_BASIC = 'http-basic';
const TYPE_MAGENTO = 'magento';
const TYPE_HTTP_HEADER = 'http-header';

public function all()
{
return $this->get('/credentials/');
}

public function show($credentialId)
{
return $this->get(sprintf('/credentials/%s/', $credentialId));
}

public function create($description, $type, $domain, $username, $credential)
{
return $this->post('/credentials/', [
'description' => $description,
'type' => $type,
'domain' => $domain,
'username' => $username,
'credential' => $credential,
]);
}

public function update($credentialId, $type, $username, $credential)
{
return $this->put(sprintf('/credentials/%s/', $credentialId), [
'username' => $username,
'credential' => $credential,
'type' => $type,
]);
}

public function remove($credentialId)
{
return $this->delete(sprintf('/credentials/%s/', $credentialId));
}
}
30 changes: 30 additions & 0 deletions src/Api/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,34 @@ public function all(array $filters = [])

return $this->get('/packages/', $filters);
}

public function show($packageName)
{
return $this->get(sprintf('/packages/%s/', $packageName));
}

public function createVcsPackage($url, $credentials = null)
{
return $this->post('/packages/', ['repoType' => 'vcs', 'repoUrl' => $url, 'credentials' => $credentials]);
}

public function createCustomPackage($customJson, $credentials = null)
{
return $this->post('/packages/', ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentials]);
}

public function updateVcsPackage($packageName, $url, $credentials = null)
{
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'vcs', 'repoUrl' => $url, 'credentials' => $credentials]);
}

public function updateCustomPackage($packageName, $customJson, $credentials = null)
{
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentials]);
}

public function remove($packageName)
{
return $this->delete(sprintf('/packages/%s/', $packageName));
}
}
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function authenticate($token, $secret)
$this->httpClientBuilder->addPlugin(new RequestSignature($token, $secret));
}

public function credentials()
{
return new Api\Credentials($this, $this->responseMediator);
}

public function customers()
{
return new Api\Customers($this, $this->responseMediator);
Expand Down
114 changes: 114 additions & 0 deletions tests/Api/CredentialsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace PrivatePackagist\ApiClient\Api;

class CredentialsTest extends ApiTestCase
{
public function testAll()
{
$expected = [
[
'id' => 1,
'description' => 'My secret credential',
'domain' => 'localhost',
'username' => 'username',
'credential' => 'password',
'type' => 'http-basic',
],
];

/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with($this->equalTo('/credentials/'))
->will($this->returnValue($expected));

$this->assertSame($expected, $api->all());
}

public function testShow()
{
$expected = [
'id' => 1,
'description' => 'My secret credential',
'domain' => 'localhost',
'username' => 'username',
'credential' => 'password',
'type' => 'http-basic',
];

/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with($this->equalTo('/credentials/1/'))
->will($this->returnValue($expected));

$this->assertSame($expected, $api->show(1));
}

public function testCreate()
{
$expected = [
'id' => 1,
'description' => 'My secret credential',
'domain' => 'localhost',
'username' => 'username',
'credential' => 'password',
'type' => 'http-basic',
];

/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with($this->equalTo('/credentials/'), $this->equalTo(['domain' => 'localhost', 'description' => 'My secret credential', 'type' => 'http-basic', 'username' => 'username', 'credential' => 'password']))
->will($this->returnValue($expected));

$this->assertSame($expected, $api->create('My secret credential', 'http-basic', 'localhost', 'username', 'password'));
}

public function testUpdate()
{
$expected = [
'id' => 1,
'description' => 'My secret credential',
'domain' => 'localhost',
'username' => 'username',
'credential' => 'password',
'type' => 'http-basic',
];

/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with($this->equalTo('/credentials/1/'), $this->equalTo(['type' => 'http-basic', 'username' => 'username', 'credential' => 'password']))
->will($this->returnValue($expected));

$this->assertSame($expected, $api->update(1, 'http-basic', 'username', 'password'));
}

public function testRemove()
{
$expected = [];

/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with($this->equalTo('/credentials/1/'))
->will($this->returnValue($expected));

$this->assertSame($expected, $api->remove(1));
}

/**
* @return string
*/
protected function getApiClass()
{
return Credentials::class;
}
}
Loading