RejectedPromise.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Http\Promise;
  3. /**
  4. * A rejected promise.
  5. *
  6. * @author Joel Wurtz <joel.wurtz@gmail.com>
  7. */
  8. final class RejectedPromise implements Promise
  9. {
  10. /**
  11. * @var \Exception
  12. */
  13. private $exception;
  14. /**
  15. * @param \Exception $exception
  16. */
  17. public function __construct(\Exception $exception)
  18. {
  19. $this->exception = $exception;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function then(callable $onFulfilled = null, callable $onRejected = null)
  25. {
  26. if (null === $onRejected) {
  27. return $this;
  28. }
  29. try {
  30. return new FulfilledPromise($onRejected($this->exception));
  31. } catch (\Exception $e) {
  32. return new self($e);
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getState()
  39. {
  40. return Promise::REJECTED;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function wait($unwrap = true)
  46. {
  47. if ($unwrap) {
  48. throw $this->exception;
  49. }
  50. }
  51. }