vendor/guzzlehttp/promises/src/RejectedPromise.php line 42

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