| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 4 |
|
| 5 |
/** |
| 6 |
* A promise that has been fulfilled. |
| 7 |
* |
| 8 |
* Thenning off of this promise will invoke the onFulfilled callback |
| 9 |
* immediately and ignore other callbacks. |
| 10 |
*/ |
| 11 |
class FulfilledPromise implements PromiseInterface |
| 12 |
{ |
| 13 |
private $value; |
| 14 |
public function __construct($value) |
| 15 |
{ |
| 16 |
if (\is_object($value) && \method_exists($value, 'then')) { |
| 17 |
throw new \InvalidArgumentException('You cannot create a FulfilledPromise with a promise.'); |
| 18 |
} |
| 19 |
$this->value = $value; |
| 20 |
} |
| 21 |
public function then(callable $onFulfilled = null, callable $onRejected = null) |
| 22 |
{ |
| 23 |
// Return itself if there is no onFulfilled function. |
| 24 |
if (!$onFulfilled) { |
| 25 |
return $this; |
| 26 |
} |
| 27 |
$queue = Utils::queue(); |
| 28 |
$p = new Promise([$queue, 'run']); |
| 29 |
$value = $this->value; |
| 30 |
$queue->add(static function () use($p, $value, $onFulfilled) { |
| 31 |
if (Is::pending($p)) { |
| 32 |
try { |
| 33 |
$p->resolve($onFulfilled($value)); |
| 34 |
} catch (\Throwable $e) { |
| 35 |
$p->reject($e); |
| 36 |
} catch (\Exception $e) { |
| 37 |
$p->reject($e); |
| 38 |
} |
| 39 |
} |
| 40 |
}); |
| 41 |
return $p; |
| 42 |
} |
| 43 |
public function otherwise(callable $onRejected) |
| 44 |
{ |
| 45 |
return $this->then(null, $onRejected); |
| 46 |
} |
| 47 |
public function wait($unwrap = \true, $defaultDelivery = null) |
| 48 |
{ |
| 49 |
return $unwrap ? $this->value : null; |
| 50 |
} |
| 51 |
public function getState() |
| 52 |
{ |
| 53 |
return self::FULFILLED; |
| 54 |
} |
| 55 |
public function resolve($value) |
| 56 |
{ |
| 57 |
if ($value !== $this->value) { |
| 58 |
throw new \LogicException("Cannot resolve a fulfilled promise"); |
| 59 |
} |
| 60 |
} |
| 61 |
public function reject($reason) |
| 62 |
{ |
| 63 |
throw new \LogicException("Cannot reject a fulfilled promise"); |
| 64 |
} |
| 65 |
public function cancel() |
| 66 |
{ |
| 67 |
// pass |
| 68 |
} |
| 69 |
} |
| 70 |
|