PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.6.1
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.6.1
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / promises / src / RejectedPromise.php

RejectedPromise.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.6.1, at vendor/guzzlehttp/promises/src/RejectedPromise.php

88 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
88