HttpFulfilledPromise.php 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Http\Client\Promise;
  3. use Http\Client\Exception;
  4. use Http\Promise\Promise;
  5. use Psr\Http\Message\ResponseInterface;
  6. final class HttpFulfilledPromise implements Promise
  7. {
  8. /**
  9. * @var ResponseInterface
  10. */
  11. private $response;
  12. public function __construct(ResponseInterface $response)
  13. {
  14. $this->response = $response;
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function then(callable $onFulfilled = null, callable $onRejected = null)
  20. {
  21. if (null === $onFulfilled) {
  22. return $this;
  23. }
  24. try {
  25. return new self($onFulfilled($this->response));
  26. } catch (Exception $e) {
  27. return new HttpRejectedPromise($e);
  28. }
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getState()
  34. {
  35. return Promise::FULFILLED;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function wait($unwrap = true)
  41. {
  42. if ($unwrap) {
  43. return $this->response;
  44. }
  45. }
  46. }