Skip to content

Commit 850858e

Browse files
committed
Initial error handling
1 parent 044cbc4 commit 850858e

File tree

8 files changed

+97
-4
lines changed

8 files changed

+97
-4
lines changed

src/Api/Customers.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PrivatePackagist\ApiClient\Api;
44

5+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
6+
57
class Customers extends AbstractApi
68
{
79
public function all()
@@ -26,6 +28,12 @@ public function listPackages($customerId)
2628

2729
public function addPackages($customerId, array $packages)
2830
{
31+
foreach ($packages as $package) {
32+
if (!isset($package['name'])) {
33+
throw new InvalidArgumentException('Parameter "name" is requried.');
34+
}
35+
}
36+
2937
return $this->post(sprintf('/customers/%s/packages/', $customerId), $packages);
3038
}
3139

src/Client.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@
55
use Http\Client\Common\Plugin;
66
use Http\Discovery\UriFactoryDiscovery;
77
use PrivatePackagist\ApiClient\HttpClient\HttpPluginClientBuilder;
8+
use PrivatePackagist\ApiClient\HttpClient\Message\ResponseMediator;
9+
use PrivatePackagist\ApiClient\HttpClient\Plugin\ExceptionThrower;
810
use PrivatePackagist\ApiClient\HttpClient\Plugin\PathPrepend;
911
use PrivatePackagist\ApiClient\HttpClient\Plugin\RequestSignature;
1012

1113
class Client
1214
{
1315
/** @var HttpPluginClientBuilder */
1416
private $httpClientBuilder;
17+
/** @var ResponseMediator */
18+
private $responseMediator;
1519

1620
/** @param string $privatePackagistUrl */
17-
public function __construct(HttpPluginClientBuilder $httpClientBuilder = null, $privatePackagistUrl = null)
21+
public function __construct(HttpPluginClientBuilder $httpClientBuilder = null, $privatePackagistUrl = null, ResponseMediator $responseMediator = null)
1822
{
1923
$this->httpClientBuilder = $builder = $httpClientBuilder ?: new HttpPluginClientBuilder();
2024
$privatePackagistUrl = $privatePackagistUrl ? : 'https://packagist.com';
25+
$this->responseMediator = $responseMediator ? $responseMediator : new ResponseMediator();
2126

2227
$builder->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($privatePackagistUrl)));
2328
$builder->addPlugin(new PathPrepend('/api'));
2429
$builder->addPlugin(new Plugin\HeaderDefaultsPlugin([
25-
'User-Agent' => 'php-private-packagist-api (http://github.com/packagist/private-packagist-api)', // @todo
30+
'User-Agent' => 'php-private-packagist-api (https://github.com/packagist/private-packagist-api-client)',
2631
]));
32+
$builder->addPlugin(new ExceptionThrower($this->responseMediator));
2733
}
2834

2935
/**
@@ -38,12 +44,12 @@ public function authenticate($token, $secret)
3844

3945
public function customers()
4046
{
41-
return new Api\Customers($this);
47+
return new Api\Customers($this, $this->responseMediator);
4248
}
4349

4450
public function packages()
4551
{
46-
return new Api\Packages($this);
52+
return new Api\Packages($this, $this->responseMediator);
4753
}
4854

4955
public function getHttpClient()

src/Exception/ErrorException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Exception;
4+
5+
class ErrorException extends \ErrorException implements ExceptionInterface
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Exception;
4+
5+
interface ExceptionInterface
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Exception;
4+
5+
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
6+
{
7+
}

src/Exception/RuntimeException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Exception;
4+
5+
class RuntimeException extends \RuntimeException implements ExceptionInterface
6+
{
7+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\HttpClient\Plugin;
4+
5+
use Http\Client\Common\Plugin;
6+
use PrivatePackagist\ApiClient\Exception\ErrorException;
7+
use PrivatePackagist\ApiClient\Exception\RuntimeException;
8+
use PrivatePackagist\ApiClient\HttpClient\Message\ResponseMediator;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
class ExceptionThrower implements Plugin
13+
{
14+
/** @var ResponseMediator */
15+
private $responseMediator;
16+
17+
public function __construct(ResponseMediator $responseMediator = null)
18+
{
19+
$this->responseMediator = $responseMediator ? $responseMediator : new ResponseMediator();
20+
}
21+
22+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
23+
{
24+
return $next($request)->then(function (ResponseInterface $response) {
25+
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {
26+
return $response;
27+
}
28+
29+
$content = $this->responseMediator->getContent($response);
30+
if (is_array($content) && isset($content['message'])) {
31+
if ($response->getStatusCode() === 400) {
32+
throw new ErrorException($content['message'], $response->getStatusCode());
33+
}
34+
}
35+
36+
throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
37+
});
38+
}
39+
}

tests/Api/CustomersTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ public function testAddPackages()
9898
$this->assertSame($expected, $api->addPackages(1, $packages));
9999
}
100100

101+
/**
102+
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
103+
* @expectedExceptionMessage Parameter "name" is requried.
104+
*/
105+
public function testAddPackagesMissingName()
106+
{
107+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
108+
$api = $this->getApiMock();
109+
110+
$api->addPackages(1, [[]]);
111+
}
112+
101113
public function testRemovePackage()
102114
{
103115
$expected = '';

0 commit comments

Comments
 (0)