1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tidal/WampWatch package. |
5
|
|
|
* (c) 2016 Timo Michna <timomichna/yahoo.de> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tidal\WampWatch\Adapter\React; |
13
|
|
|
|
14
|
|
|
use Tidal\WampWatch\Async\PromiseInterface; |
15
|
|
|
use React\Promise\ExtendedPromiseInterface as ReactPromise; |
16
|
|
|
|
17
|
|
|
class PromiseAdapter implements PromiseInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ReactPromise |
21
|
|
|
*/ |
22
|
|
|
private $adaptee; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* PromiseAdapter constructor. |
26
|
|
|
* |
27
|
|
|
* @param ReactPromise $adaptee |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ReactPromise $adaptee) |
30
|
|
|
{ |
31
|
|
|
$this->setAdaptee($adaptee); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ReactPromise $adaptee |
36
|
|
|
*/ |
37
|
|
|
private function setAdaptee(ReactPromise $adaptee) |
38
|
|
|
{ |
39
|
|
|
$this->adaptee = $adaptee; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return ReactPromise |
44
|
|
|
*/ |
45
|
|
|
public function getAdaptee() |
46
|
|
|
{ |
47
|
|
|
return $this->adaptee; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param callable|null $onFulfilled |
52
|
|
|
* @param callable|null $onRejected |
53
|
|
|
* @param callable|null $onProgress |
54
|
|
|
* |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->adaptee->then(function () use ($onFulfilled) { |
60
|
|
|
if ($onFulfilled !== null) { |
61
|
|
|
return call_user_func_array($onFulfilled, func_get_args()); |
62
|
|
|
} |
63
|
|
|
}, function () use ($onRejected) { |
64
|
|
|
if ($onRejected !== null) { |
65
|
|
|
return call_user_func_array($onRejected, func_get_args()); |
66
|
|
|
} |
67
|
|
|
}, function () use ($onProgress) { |
68
|
|
|
if ($onProgress !== null) { |
69
|
|
|
return call_user_func_array($onProgress, func_get_args()); |
70
|
|
|
} |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param callable|null $onFulfilled |
78
|
|
|
* @param callable|null $onRejected |
79
|
|
|
* @param callable|null $onProgress |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this->adaptee->done(function () use ($onFulfilled) { |
86
|
|
|
if ($onFulfilled !== null) { |
87
|
|
|
return call_user_func_array($onFulfilled, func_get_args()); |
88
|
|
|
} |
89
|
|
|
}, function () use ($onRejected) { |
90
|
|
|
if ($onRejected !== null) { |
91
|
|
|
return call_user_func_array($onRejected, func_get_args()); |
92
|
|
|
} |
93
|
|
|
}, function () use ($onProgress) { |
94
|
|
|
if ($onProgress !== null) { |
95
|
|
|
return call_user_func_array($onProgress, func_get_args()); |
96
|
|
|
} |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param callable $onRejected |
104
|
|
|
* |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
|
|
public function otherwise(callable $onRejected) |
108
|
|
|
{ |
109
|
|
|
$this->adaptee->otherwise(function () use ($onRejected) { |
110
|
|
|
return call_user_func_array($onRejected, func_get_args()); |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param callable $onAlways |
118
|
|
|
* |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
|
|
public function always(callable $onAlways) |
122
|
|
|
{ |
123
|
|
|
$this->adaptee->always(function () use ($onAlways) { |
124
|
|
|
return call_user_func_array($onAlways, func_get_args()); |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param callable $onProgress |
132
|
|
|
* |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
|
|
public function progress(callable $onProgress) |
136
|
|
|
{ |
137
|
|
|
$this->adaptee->progress(function () use ($onProgress) { |
138
|
|
|
return call_user_func_array($onProgress, func_get_args()); |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.