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