|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Http\Client; |
| 4 | + |
| 5 | +use Psr\Http\Message\ResponseInterface; |
| 6 | + |
| 7 | +/** |
| 8 | + * Promise represents a response that may not be available yet, but will be resolved at some point in future. |
| 9 | + * It acts like a proxy to the actual response. |
| 10 | + * |
| 11 | + * This interface is an extension of the promises/a+ specification https://promisesaplus.com/ |
| 12 | + * Value is replaced by an object where its class implement a Psr\Http\Message\RequestInterface. |
| 13 | + * Reason is replaced by an object where its class implement a Http\Client\Exception. |
| 14 | + * |
| 15 | + * @author Joel Wurtz <[email protected]> |
| 16 | + */ |
| 17 | +interface Promise |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Pending state, promise has not been fulfilled or rejected. |
| 21 | + */ |
| 22 | + const PENDING = "pending"; |
| 23 | + |
| 24 | + /** |
| 25 | + * Fulfilled state, promise has been fulfilled with a ResponseInterface object. |
| 26 | + */ |
| 27 | + const FULFILLED = "fulfilled"; |
| 28 | + |
| 29 | + /** |
| 30 | + * Rejected state, promise has been rejected with an Exception object. |
| 31 | + */ |
| 32 | + const REJECTED = "rejected"; |
| 33 | + |
| 34 | + /** |
| 35 | + * Add behavior for when the promise is resolved or rejected (response will be available, or error happens). |
| 36 | + * |
| 37 | + * If you do not care about one of the cases, you can set the corresponding callable to null |
| 38 | + * The callback will be called when the response or exception arrived and never more than once. |
| 39 | + * |
| 40 | + * @param callable $onFulfilled Called when a response will be available. |
| 41 | + * @param callable $onRejected Called when an error happens. |
| 42 | + * |
| 43 | + * You must always return the Response in the interface or throw an Exception. |
| 44 | + * |
| 45 | + * @return Promise Always returns a new promise which is resolved with value of the executed callback (onFulfilled / onRejected). |
| 46 | + */ |
| 47 | + public function then(callable $onFulfilled = null, callable $onRejected = null); |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the state of the promise, one of PENDING, FULFILLED or REJECTED |
| 51 | + * |
| 52 | + * @return int |
| 53 | + */ |
| 54 | + public function getState(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Return the value of the promise (fulfilled). |
| 58 | + * |
| 59 | + * @return ResponseInterface Response Object only when the Promise is fulfilled. |
| 60 | + * |
| 61 | + * @throws \LogicException When the promise is not fulfilled. |
| 62 | + */ |
| 63 | + public function getResponse(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the reason why the promise was rejected.. |
| 67 | + * |
| 68 | + * If the exception is an instance of Http\Client\Exception\HttpException it will contain |
| 69 | + * the response object with the status code and the http reason. |
| 70 | + * |
| 71 | + * @return Exception Exception Object only when the Promise is rejected. |
| 72 | + * |
| 73 | + * @throws \LogicException When the promise is not rejected. |
| 74 | + */ |
| 75 | + public function getException(); |
| 76 | + |
| 77 | + /** |
| 78 | + * Wait for the promise to be fulfilled or rejected. |
| 79 | + * |
| 80 | + * When this method returns, the request has been resolved and the appropriate callable has terminated. |
| 81 | + */ |
| 82 | + public function wait(); |
| 83 | +} |
0 commit comments