PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / php-http / client-common / src / Deferred.php
ameliabooking / vendor / php-http / client-common / src Last commit date
Exception 3 years ago HttpClientPool 3 years ago Plugin 3 years ago BatchClient.php 3 years ago BatchResult.php 3 years ago Deferred.php 3 years ago EmulatedHttpAsyncClient.php 3 years ago EmulatedHttpClient.php 3 years ago FlexibleHttpClient.php 3 years ago HttpAsyncClientDecorator.php 3 years ago HttpAsyncClientEmulator.php 3 years ago HttpClientDecorator.php 3 years ago HttpClientEmulator.php 3 years ago HttpClientPool.php 3 years ago HttpClientPoolItem.php 3 years ago HttpClientRouter.php 3 years ago HttpMethodsClient.php 3 years ago Plugin.php 3 years ago PluginClient.php 3 years ago PluginClientFactory.php 3 years ago VersionBridgeClient.php 3 years ago
Deferred.php
132 lines
1 <?php
2
3 namespace AmeliaHttp\Client\Common;
4
5 use AmeliaHttp\Client\Exception;
6 use AmeliaHttp\Promise\Promise;
7 use AmeliaPsr\Http\Message\ResponseInterface;
8
9 /**
10 * A deferred allow to return a promise which has not been resolved yet.
11 */
12 class Deferred implements Promise
13 {
14 private $value;
15
16 private $failure;
17
18 private $state;
19
20 private $waitCallback;
21
22 private $onFulfilledCallbacks;
23
24 private $onRejectedCallbacks;
25
26 public function __construct(callable $waitCallback)
27 {
28 $this->waitCallback = $waitCallback;
29 $this->state = Promise::PENDING;
30 $this->onFulfilledCallbacks = [];
31 $this->onRejectedCallbacks = [];
32 }
33
34 /**
35 * {@inheritdoc}
36 */
37 public function then(callable $onFulfilled = null, callable $onRejected = null)
38 {
39 $deferred = new self($this->waitCallback);
40
41 $this->onFulfilledCallbacks[] = function (ResponseInterface $response) use ($onFulfilled, $deferred) {
42 try {
43 if (null !== $onFulfilled) {
44 $response = $onFulfilled($response);
45 }
46 $deferred->resolve($response);
47 } catch (Exception $exception) {
48 $deferred->reject($exception);
49 }
50 };
51
52 $this->onRejectedCallbacks[] = function (Exception $exception) use ($onRejected, $deferred) {
53 try {
54 if (null !== $onRejected) {
55 $response = $onRejected($exception);
56 $deferred->resolve($response);
57
58 return;
59 }
60 $deferred->reject($exception);
61 } catch (Exception $newException) {
62 $deferred->reject($newException);
63 }
64 };
65
66 return $deferred;
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function getState()
73 {
74 return $this->state;
75 }
76
77 /**
78 * Resolve this deferred with a Response.
79 */
80 public function resolve(ResponseInterface $response)
81 {
82 if (self::PENDING !== $this->state) {
83 return;
84 }
85
86 $this->value = $response;
87 $this->state = self::FULFILLED;
88
89 foreach ($this->onFulfilledCallbacks as $onFulfilledCallback) {
90 $onFulfilledCallback($response);
91 }
92 }
93
94 /**
95 * Reject this deferred with an Exception.
96 */
97 public function reject(Exception $exception)
98 {
99 if (self::PENDING !== $this->state) {
100 return;
101 }
102
103 $this->failure = $exception;
104 $this->state = self::REJECTED;
105
106 foreach ($this->onRejectedCallbacks as $onRejectedCallback) {
107 $onRejectedCallback($exception);
108 }
109 }
110
111 /**
112 * {@inheritdoc}
113 */
114 public function wait($unwrap = true)
115 {
116 if (self::PENDING === $this->state) {
117 $callback = $this->waitCallback;
118 $callback();
119 }
120
121 if (!$unwrap) {
122 return;
123 }
124
125 if (self::FULFILLED === $this->state) {
126 return $this->value;
127 }
128
129 throw $this->failure;
130 }
131 }
132