Open Journal Systems  3.3.0
guzzlehttp/promises/src/RejectedPromise.php
1 <?php
2 namespace GuzzleHttp\Promise;
3 
11 {
12  private $reason;
13 
14  public function __construct($reason)
15  {
16  if (method_exists($reason, 'then')) {
17  throw new \InvalidArgumentException(
18  'You cannot create a RejectedPromise with a promise.');
19  }
20 
21  $this->reason = $reason;
22  }
23 
24  public function then(
25  callable $onFulfilled = null,
26  callable $onRejected = null
27  ) {
28  // If there's no onRejected callback then just return self.
29  if (!$onRejected) {
30  return $this;
31  }
32 
33  $queue = queue();
34  $reason = $this->reason;
35  $p = new Promise([$queue, 'run']);
36  $queue->add(static function () use ($p, $reason, $onRejected) {
37  if ($p->getState() === self::PENDING) {
38  try {
39  // Return a resolved promise if onRejected does not throw.
40  $p->resolve($onRejected($reason));
41  } catch (\Throwable $e) {
42  // onRejected threw, so return a rejected promise.
43  $p->reject($e);
44  } catch (\Exception $e) {
45  // onRejected threw, so return a rejected promise.
46  $p->reject($e);
47  }
48  }
49  });
50 
51  return $p;
52  }
53 
54  public function otherwise(callable $onRejected)
55  {
56  return $this->then(null, $onRejected);
57  }
58 
59  public function wait($unwrap = true, $defaultDelivery = null)
60  {
61  if ($unwrap) {
62  throw exception_for($this->reason);
63  }
64  }
65 
66  public function getState()
67  {
68  return self::REJECTED;
69  }
70 
71  public function resolve($value)
72  {
73  throw new \LogicException("Cannot resolve a rejected promise");
74  }
75 
76  public function reject($reason)
77  {
78  if ($reason !== $this->reason) {
79  throw new \LogicException("Cannot reject a rejected promise");
80  }
81  }
82 
83  public function cancel()
84  {
85  // pass
86  }
87 }
GuzzleHttp\Promise\PromiseInterface
Definition: PromiseInterface.php:13
GuzzleHttp\Promise\RejectedPromise\cancel
cancel()
Definition: guzzlehttp/promises/src/RejectedPromise.php:83
GuzzleHttp\Promise\Promise\then
then(callable $onFulfilled=null, callable $onRejected=null)
Definition: guzzlehttp/promises/src/Promise.php:30
GuzzleHttp\Promise\queue
queue(TaskQueueInterface $assign=null)
Definition: guzzlehttp/promises/src/functions.php:21
GuzzleHttp\Promise\RejectedPromise\reject
reject($reason)
Definition: guzzlehttp/promises/src/RejectedPromise.php:76
GuzzleHttp\Promise\RejectedPromise\__construct
__construct($reason)
Definition: guzzlehttp/promises/src/RejectedPromise.php:14
GuzzleHttp\Promise\RejectedPromise\otherwise
otherwise(callable $onRejected)
Definition: guzzlehttp/promises/src/RejectedPromise.php:54
GuzzleHttp\Promise\PromiseInterface\REJECTED
const REJECTED
Definition: PromiseInterface.php:17
GuzzleHttp\Promise\RejectedPromise\resolve
resolve($value)
Definition: guzzlehttp/promises/src/RejectedPromise.php:71
GuzzleHttp\Promise\Promise
Definition: guzzlehttp/promises/src/Promise.php:9
GuzzleHttp\Promise\RejectedPromise\then
then(callable $onFulfilled=null, callable $onRejected=null)
Definition: guzzlehttp/promises/src/RejectedPromise.php:24
GuzzleHttp\Promise\RejectedPromise
Definition: guzzlehttp/promises/src/RejectedPromise.php:10
GuzzleHttp\Promise
Definition: AggregateException.php:2
GuzzleHttp\Promise\RejectedPromise\getState
getState()
Definition: guzzlehttp/promises/src/RejectedPromise.php:66
GuzzleHttp\Promise\RejectedPromise\wait
wait($unwrap=true, $defaultDelivery=null)
Definition: guzzlehttp/promises/src/RejectedPromise.php:59
GuzzleHttp\Promise\exception_for
exception_for($reason)
Definition: guzzlehttp/promises/src/functions.php:108