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