IPromise.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCP\Http\Client;
  23. use Exception;
  24. use LogicException;
  25. /**
  26. * A wrapper around Guzzle's PromiseInterface
  27. * @see \GuzzleHttp\Promise\PromiseInterface
  28. * @since 28.0.0
  29. */
  30. interface IPromise {
  31. /**
  32. * @since 28.0.0
  33. */
  34. public const STATE_PENDING = 'pending';
  35. /**
  36. * @since 28.0.0
  37. */
  38. public const STATE_FULFILLED = 'fulfilled';
  39. /**
  40. * @since 28.0.0
  41. */
  42. public const STATE_REJECTED = 'rejected';
  43. /**
  44. * Appends fulfillment and rejection handlers to the promise, and returns
  45. * a new promise resolving to the return value of the called handler.
  46. *
  47. * @param ?callable(IResponse): void $onFulfilled Invoked when the promise fulfills. Gets an \OCP\Http\Client\IResponse passed in as argument
  48. * @param ?callable(Exception): void $onRejected Invoked when the promise is rejected. Gets an \Exception passed in as argument
  49. *
  50. * @return IPromise
  51. * @since 28.0.0
  52. */
  53. public function then(
  54. ?callable $onFulfilled = null,
  55. ?callable $onRejected = null,
  56. ): IPromise;
  57. /**
  58. * Get the state of the promise ("pending", "rejected", or "fulfilled").
  59. *
  60. * The three states can be checked against the constants defined:
  61. * STATE_PENDING, STATE_FULFILLED, and STATE_REJECTED.
  62. *
  63. * @return self::STATE_*
  64. * @since 28.0.0
  65. */
  66. public function getState(): string;
  67. /**
  68. * Cancels the promise if possible.
  69. *
  70. * @link https://github.com/promises-aplus/cancellation-spec/issues/7
  71. * @since 28.0.0
  72. */
  73. public function cancel(): void;
  74. /**
  75. * Waits until the promise completes if possible.
  76. *
  77. * Pass $unwrap as true to unwrap the result of the promise, either
  78. * returning the resolved value or throwing the rejected exception.
  79. *
  80. * If the promise cannot be waited on, then the promise will be rejected.
  81. *
  82. * @param bool $unwrap
  83. *
  84. * @return mixed
  85. *
  86. * @throws LogicException if the promise has no wait function or if the
  87. * promise does not settle after waiting.
  88. * @since 28.0.0
  89. */
  90. public function wait(bool $unwrap = true): mixed;
  91. }