Skip to content

Commit c0d2a1d

Browse files
authored
Merge 95c829d into 5183cf8
2 parents 5183cf8 + 95c829d commit c0d2a1d

File tree

5 files changed

+146
-1
lines changed

5 files changed

+146
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## 2.0.0 - Unreleased
4+
5+
- The HttpAsyncClient now returns a HttpPromise (instead of a Promise)
6+
7+
## 1.1.0 - Unreleased
8+
9+
- Added HttpPromise interface to enforce value of rejected and resolved promise
10+
- Added HttpFulfilledPromise and HttpRejectedPromise which respect the HttpAsyncClient interface
311

412
## 1.0.0 - 2016-01-26
513

src/HttpAsyncClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface HttpAsyncClient
1919
*
2020
* @param RequestInterface $request
2121
*
22-
* @return Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception.
22+
* @return Promise Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception.
2323
*
2424
* @throws \Exception If processing the request is impossible (eg. bad configuration).
2525
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Http\Client\Promise;
4+
5+
use Http\Client\Exception;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
final class HttpFulfilledPromise implements HttpPromise
9+
{
10+
/**
11+
* @var ResponseInterface
12+
*/
13+
private $response;
14+
15+
/**
16+
* @param ResponseInterface $response
17+
*/
18+
public function __construct(ResponseInterface $response)
19+
{
20+
$this->response = $response;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function then(callable $onFulfilled = null, callable $onRejected = null)
27+
{
28+
if (null === $onFulfilled) {
29+
return $this;
30+
}
31+
32+
try {
33+
return new self($onFulfilled($this->response));
34+
} catch (Exception $e) {
35+
return new HttpRejectedPromise($e);
36+
}
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getState()
43+
{
44+
return HttpPromise::FULFILLED;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function wait($unwrap = true)
51+
{
52+
if ($unwrap) {
53+
return $this->response;
54+
}
55+
}
56+
}

src/Promise/HttpPromise.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Http\Client\Promise;
4+
5+
use Http\Client\Exception;
6+
use Http\Promise\Promise;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
interface HttpPromise extends Promise
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*
14+
* @return HttpPromise A new resolved http promise with value of the executed callback (onFulfilled / onRejected).
15+
*/
16+
public function then(callable $onFulfilled = null, callable $onRejected = null);
17+
18+
/**
19+
* {@inheritdoc}
20+
*
21+
* @return ResponseInterface Resolved response, null if $unwrap is set to false
22+
*
23+
* @throws Exception The http exception if $unwrap is set to true and the request failed.
24+
*/
25+
public function wait($unwrap = true);
26+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Http\Client\Promise;
4+
5+
use Http\Client\Exception;
6+
7+
final class HttpRejectedPromise implements HttpPromise
8+
{
9+
/**
10+
* @var Exception
11+
*/
12+
private $exception;
13+
14+
/**
15+
* @param Exception $exception
16+
*/
17+
public function __construct(Exception $exception)
18+
{
19+
$this->exception = $exception;
20+
}
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function then(callable $onFulfilled = null, callable $onRejected = null)
26+
{
27+
if (null === $onRejected) {
28+
return $this;
29+
}
30+
31+
try {
32+
return new HttpFulfilledPromise($onRejected($this->exception));
33+
} catch (Exception $e) {
34+
return new self($e);
35+
}
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getState()
42+
{
43+
return HttpPromise::REJECTED;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function wait($unwrap = true)
50+
{
51+
if ($unwrap) {
52+
throw $this->exception;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)