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
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ Returns an array of customer packages.
```php
$customerId = 42;
$packages = [
['name' => 'acme-website/package'],
[
'name' => 'acme-website/package',
'versionConstraint' => '^1.0 | ^2.0', // optional version constraint to limit updades the customer receives
'expirationDate' => (new \DateTime())->add(new \DateInterval('P1Y'))->format('c'), // optional expiration date to limit updades the customer receives
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe add automatic conversion of datetime objects?

],
];
$packages = $client->customers()->addPackages($customerId, $packages);
```
Expand All @@ -78,11 +82,24 @@ $packageName = 'acme-website/package';
$client->customers()->removePackage($customerId, $packageName);
```

#### Regenerate a customer's Composer repository token
```php
$customerId = 42;
$confirmation = [
'IConfirmOldTokenWillStopWorkingImmediately' => true,
];
$composerRepository = $client->customers()->regenerateToken($customerId, $confirmation);
```
Returns the updated Composer repository.

#### Package

##### List an organization's packages
```php
$packages = $client->packages()->all();
$filters = [
'origin' => \PrivatePackagist\ApiClient\Api\Packages::ORIGIN_PRIVATE, // optional filter to only receive packages that can be added to customers
];
$packages = $client->packages()->all($filters);
```
Returns an array of packages.

Expand Down
9 changes: 9 additions & 0 deletions src/Api/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ public function removePackage($customerId, $packageName)
{
return $this->delete(sprintf('/customers/%s/packages/%s/', $customerId, $packageName));
}

public function regenerateToken($customerId, array $confirmation)
{
if (!isset($confirmation['IConfirmOldTokenWillStopWorkingImmediately'])) {
throw new InvalidArgumentException('Confirmation is required to regenerate the Composer repository token.');
}

return $this->post(sprintf('/customers/%s/token/regenerate', $customerId), $confirmation);
}
}
26 changes: 24 additions & 2 deletions src/Api/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,32 @@

namespace PrivatePackagist\ApiClient\Api;

use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;

class Packages extends AbstractApi
{
public function all()
/**
* Packages that are mirrored from a public Composer repository like packagist.org.
*/
const ORIGIN_PUBLIC_PROXY = 'public-proxy';

/**
* Packages that are mirrored from a private Composer repository requiring authentication like repo.magento.com.
*/
const ORIGIN_PRIVATE_PROXY = 'private-proxy';

/**
* All other packages from a VCS repository or a custom JSON definition.
*/
const ORIGIN_PRIVATE = 'private';

public function all(array $filters = [])
{
return $this->get('/packages/');
$availableOrigins = [self::ORIGIN_PUBLIC_PROXY, self::ORIGIN_PRIVATE_PROXY, self::ORIGIN_PRIVATE];
if (isset($filters['origin']) && !in_array($filters['origin'], $availableOrigins, true)) {
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', $availableOrigins) . '".');
}

return $this->get('/packages/', $filters);
}
}
40 changes: 40 additions & 0 deletions tests/Api/CustomersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function testListPackages()
[
'name' => 'composer/composer',
'origin' => 'private',
'versionConstraint' => null,
'expirationDate' => null,
],
];

Expand All @@ -83,6 +85,8 @@ public function testAddPackages()
[
'name' => 'composer/composer',
'origin' => 'private',
'versionConstraint' => null,
'expirationDate' => null,
],
];

Expand Down Expand Up @@ -125,6 +129,42 @@ public function testRemovePackage()
$this->assertSame($expected, $api->removePackage(1, $packageName));
}

public function testRegenerateToken()
{
$expected = [
'url' => 'https://repo.packagist.com/acme-website/',
'user' => 'token',
'token' => 'regenerated-token',
'lastUsed' => null,
];

$confirmation = [
'IConfirmOldTokenWillStopWorkingImmediately' => true,
];

/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with($this->equalTo('/customers/1/token/regenerate'), $this->equalTo($confirmation))
->will($this->returnValue($expected));

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

/**
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
*/
public function testRegenerateTokenMissingConfirmation()
{
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->never())
->method('post');

$api->regenerateToken(1, []);
}

protected function getApiClass()
{
return Customers::class;
Expand Down
42 changes: 41 additions & 1 deletion tests/Api/PackagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function testAll()
$expected = [
[
'id' => 1,
'name' => 'composer/composer',
'name' => 'acme-website/package',
],
];

Expand All @@ -23,6 +23,46 @@ public function testAll()
$this->assertSame($expected, $api->all());
}

public function testAllWithFilters()
{
$expected = [
[
'id' => 1,
'name' => 'acme-website/package',
],
];

$filters = [
'origin' => Packages::ORIGIN_PRIVATE,
];

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

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

/**
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
*/
public function testAllWithInvalidFilters()
{
$filters = [
'origin' => 'invalid'
];

/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
$api = $this->getApiMock();
$api->expects($this->never())
->method('get');

$api->all($filters);
}

protected function getApiClass()
{
return Packages::class;
Expand Down