krz/vox-populi
A Tumblr web client.
clone: git clone https://gitbay.org/krz/vox-populi.git
main: vendor/guzzlehttp/promises/src/FulfilledPromise.php · raw
1<?php
2namespace GuzzleHttp\Promise;
3
4/**
5 * A promise that has been fulfilled.
6 *
7 * Thenning off of this promise will invoke the onFulfilled callback
8 * immediately and ignore other callbacks.
9 */
10class FulfilledPromise implements PromiseInterface
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}