1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Async\Promise; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Promise Deferred class. |
16
|
|
|
* |
17
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PromiseDeferred extends AbstractPromise implements DeferredInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DeferredInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $deferreds = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var PromiseInterface |
28
|
|
|
*/ |
29
|
|
|
private $actual = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param callable $onFulfilled |
33
|
|
|
* @param callable $onRejected |
34
|
|
|
* @param callable $onNotify |
35
|
|
|
* |
36
|
|
|
* @return PromiseInterface |
37
|
|
|
*/ |
38
|
|
|
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null) |
39
|
|
|
{ |
40
|
|
|
if ($this->state()->equals(State::PENDING())) { |
41
|
|
|
return $this->enqueue($onFulfilled, $onRejected, $onNotify)->promise(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->actual->then($onFulfilled, $onRejected, $onNotify); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function resolve($value = null) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
if ($this->state()->equals(State::PENDING())) { |
53
|
|
|
$this->actual = new FulfilledPromise($value); |
54
|
|
|
|
55
|
|
|
while (!empty($this->deferreds)) { |
56
|
|
|
/** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */ |
57
|
|
|
$deferred = array_shift($this->deferreds); |
58
|
|
|
$deferred->resolve($value); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
throw new \LogicException(\sprintf('A %s promise cannot be resolved', $this->state()->getValue())); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function reject($reason = null) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if ($this->state()->equals(State::PENDING())) { |
71
|
|
|
$this->actual = new RejectedPromise($reason); |
72
|
|
|
while (!empty($this->deferreds)) { |
73
|
|
|
/** @var \Cubiche\Core\Async\Promise\DeferredInterface $deferred */ |
74
|
|
|
$deferred = array_shift($this->deferreds); |
75
|
|
|
$deferred->reject($reason); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
throw new \LogicException(\sprintf('A %s promise cannot be resolved', $this->state()->getValue())); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function notify($state = null) |
86
|
|
|
{ |
87
|
|
|
if ($this->state()->equals(State::PENDING())) { |
88
|
|
|
foreach ($this->deferreds as $deferred) { |
89
|
|
|
$deferred->notify($state); |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
|
|
throw new \LogicException(\sprintf('A %s promise cannot be notified', $this->state()->getValue())); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function state() |
100
|
|
|
{ |
101
|
|
|
return $this->actual !== null ? $this->actual->state() : State::PENDING(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function promise() |
108
|
|
|
{ |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param callable $onFulfilled |
114
|
|
|
* @param callable $onRejected |
115
|
|
|
* @param callable $onNotify |
116
|
|
|
* |
117
|
|
|
* @return \Cubiche\Core\Async\Promise\DeferredProxy |
118
|
|
|
*/ |
119
|
|
|
private function enqueue(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null) |
120
|
|
|
{ |
121
|
|
|
$deferred = new DeferredProxy(new Deferred(), $onFulfilled, $onRejected, $onNotify, false); |
122
|
|
|
$this->deferreds[] = $deferred; |
123
|
|
|
|
124
|
|
|
return $deferred; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.