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