Skip to content

Commit ba73e96

Browse files
authored
Merge pull request #2 from packagist/t/new-api-methods
Add new token endpoint and additional params for existing endpoints
2 parents 4f091c3 + 8d6fbe4 commit ba73e96

File tree

5 files changed

+133
-5
lines changed

5 files changed

+133
-5
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ Returns an array of customer packages.
6565
```php
6666
$customerId = 42;
6767
$packages = [
68-
['name' => 'acme-website/package'],
68+
[
69+
'name' => 'acme-website/package',
70+
'versionConstraint' => '^1.0 | ^2.0', // optional version constraint to limit updades the customer receives
71+
'expirationDate' => (new \DateTime())->add(new \DateInterval('P1Y'))->format('c'), // optional expiration date to limit updades the customer receives
72+
],
6973
];
7074
$packages = $client->customers()->addPackages($customerId, $packages);
7175
```
@@ -78,11 +82,24 @@ $packageName = 'acme-website/package';
7882
$client->customers()->removePackage($customerId, $packageName);
7983
```
8084

85+
#### Regenerate a customer's Composer repository token
86+
```php
87+
$customerId = 42;
88+
$confirmation = [
89+
'IConfirmOldTokenWillStopWorkingImmediately' => true,
90+
];
91+
$composerRepository = $client->customers()->regenerateToken($customerId, $confirmation);
92+
```
93+
Returns the updated Composer repository.
94+
8195
#### Package
8296

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

src/Api/Customers.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,13 @@ public function removePackage($customerId, $packageName)
4141
{
4242
return $this->delete(sprintf('/customers/%s/packages/%s/', $customerId, $packageName));
4343
}
44+
45+
public function regenerateToken($customerId, array $confirmation)
46+
{
47+
if (!isset($confirmation['IConfirmOldTokenWillStopWorkingImmediately'])) {
48+
throw new InvalidArgumentException('Confirmation is required to regenerate the Composer repository token.');
49+
}
50+
51+
return $this->post(sprintf('/customers/%s/token/regenerate', $customerId), $confirmation);
52+
}
4453
}

src/Api/Packages.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,32 @@
22

33
namespace PrivatePackagist\ApiClient\Api;
44

5+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
6+
57
class Packages extends AbstractApi
68
{
7-
public function all()
9+
/**
10+
* Packages that are mirrored from a public Composer repository like packagist.org.
11+
*/
12+
const ORIGIN_PUBLIC_PROXY = 'public-proxy';
13+
14+
/**
15+
* Packages that are mirrored from a private Composer repository requiring authentication like repo.magento.com.
16+
*/
17+
const ORIGIN_PRIVATE_PROXY = 'private-proxy';
18+
19+
/**
20+
* All other packages from a VCS repository or a custom JSON definition.
21+
*/
22+
const ORIGIN_PRIVATE = 'private';
23+
24+
public function all(array $filters = [])
825
{
9-
return $this->get('/packages/');
26+
$availableOrigins = [self::ORIGIN_PUBLIC_PROXY, self::ORIGIN_PRIVATE_PROXY, self::ORIGIN_PRIVATE];
27+
if (isset($filters['origin']) && !in_array($filters['origin'], $availableOrigins, true)) {
28+
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', $availableOrigins) . '".');
29+
}
30+
31+
return $this->get('/packages/', $filters);
1032
}
1133
}

tests/Api/CustomersTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function testListPackages()
6464
[
6565
'name' => 'composer/composer',
6666
'origin' => 'private',
67+
'versionConstraint' => null,
68+
'expirationDate' => null,
6769
],
6870
];
6971

@@ -83,6 +85,8 @@ public function testAddPackages()
8385
[
8486
'name' => 'composer/composer',
8587
'origin' => 'private',
88+
'versionConstraint' => null,
89+
'expirationDate' => null,
8690
],
8791
];
8892

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

132+
public function testRegenerateToken()
133+
{
134+
$expected = [
135+
'url' => 'https://repo.packagist.com/acme-website/',
136+
'user' => 'token',
137+
'token' => 'regenerated-token',
138+
'lastUsed' => null,
139+
];
140+
141+
$confirmation = [
142+
'IConfirmOldTokenWillStopWorkingImmediately' => true,
143+
];
144+
145+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
146+
$api = $this->getApiMock();
147+
$api->expects($this->once())
148+
->method('post')
149+
->with($this->equalTo('/customers/1/token/regenerate'), $this->equalTo($confirmation))
150+
->will($this->returnValue($expected));
151+
152+
$this->assertSame($expected, $api->regenerateToken(1, $confirmation));
153+
}
154+
155+
/**
156+
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
157+
*/
158+
public function testRegenerateTokenMissingConfirmation()
159+
{
160+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
161+
$api = $this->getApiMock();
162+
$api->expects($this->never())
163+
->method('post');
164+
165+
$api->regenerateToken(1, []);
166+
}
167+
128168
protected function getApiClass()
129169
{
130170
return Customers::class;

tests/Api/PackagesTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function testAll()
99
$expected = [
1010
[
1111
'id' => 1,
12-
'name' => 'composer/composer',
12+
'name' => 'acme-website/package',
1313
],
1414
];
1515

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

26+
public function testAllWithFilters()
27+
{
28+
$expected = [
29+
[
30+
'id' => 1,
31+
'name' => 'acme-website/package',
32+
],
33+
];
34+
35+
$filters = [
36+
'origin' => Packages::ORIGIN_PRIVATE,
37+
];
38+
39+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
40+
$api = $this->getApiMock();
41+
$api->expects($this->once())
42+
->method('get')
43+
->with($this->equalTo('/packages/'), $this->equalTo($filters))
44+
->will($this->returnValue($expected));
45+
46+
$this->assertSame($expected, $api->all($filters));
47+
}
48+
49+
/**
50+
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
51+
*/
52+
public function testAllWithInvalidFilters()
53+
{
54+
$filters = [
55+
'origin' => 'invalid'
56+
];
57+
58+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
59+
$api = $this->getApiMock();
60+
$api->expects($this->never())
61+
->method('get');
62+
63+
$api->all($filters);
64+
}
65+
2666
protected function getApiClass()
2767
{
2868
return Packages::class;

0 commit comments

Comments
 (0)