PromiseFulfilled::then()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 20
loc 20
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 3
crap 4.0312
1
<?php
2
3
namespace Dazzle\Promise;
4
5
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException;
6
use Error;
7
use Exception;
8
9
class PromiseFulfilled implements PromiseInterface
10
{
11
    /**
12
     * @var mixed|null
13
     */
14
    protected $value;
15
16
    /**
17
     * @param mixed|null $value
18
     * @throws InvalidArgumentException
19
     */
20 316
    public function __construct($value = null)
21
    {
22 316
        if ($value instanceof PromiseInterface)
23
        {
24 1
            throw new InvalidArgumentException(
25 1
                'You cannot create PromiseFulfilled with a promise. Use Promise::doResolve($promiseOrValue) instead.'
26
            );
27
        }
28
29 316
        $this->value = $value;
30 316
    }
31
32
    /**
33
     *
34
     */
35 290
    public function __destruct()
36
    {
37 290
        unset($this->value);
38 290
    }
39
40
    /**
41
     * @override
42
     * @inheritDoc
43
     */
44 222 View Code Duplication
    public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
45
    {
46 222
        if (null === $onFulfilled)
47
        {
48 12
            return $this;
49
        }
50
51
        try
52
        {
53 215
            return Promise::doResolve($onFulfilled($this->getValue()));
54
        }
55 13
        catch (Error $ex)
56
        {
57
            return new PromiseRejected($ex);
58
        }
59 13
        catch (Exception $ex)
60
        {
61 13
            return new PromiseRejected($ex);
62
        }
63
    }
64
65
    /**
66
     * @override
67
     * @inheritDoc
68
     */
69 110
    public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null)
70
    {
71 110
        if (null === $onFulfilled)
72
        {
73
            return;
74
        }
75
76 110
        $result = $onFulfilled($this->getValue());
77
78 102
        if ($result instanceof PromiseInterface)
79
        {
80 5
            $result->done();
81
        }
82 97
    }
83
84
    /**
85
     * @override
86
     * @inheritDoc
87
     */
88 2
    public function spread(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null)
89
    {
90 2
        return $this->then(
91
            function($values) use($onFulfilled) {
92 2
                return $onFulfilled(...((array) $values));
93 2
            }
94
        );
95
    }
96
97
    /**
98
     * @override
99
     * @inheritDoc
100
     */
101 1
    public function success(callable $onSuccess)
102
    {
103 1
        return $this->then($onSuccess);
104
    }
105
106
    /**
107
     * @override
108
     * @inheritDoc
109
     */
110 2
    public function failure(callable $onFailure)
111
    {
112 2
        return $this->then(null, $onFailure);
113
    }
114
115
    /**
116
     * @override
117
     * @inheritDoc
118
     */
119 1
    public function abort(callable $onCancel)
120
    {
121 1
        return $this->then(null, null, $onCancel);
122
    }
123
124
    /**
125
     * @override
126
     * @inheritDoc
127
     */
128 5
    public function always(callable $onFulfilledOrRejected)
129
    {
130 5
        return $this->then(
131
            function($value) use($onFulfilledOrRejected) {
132 4
                return Promise::doResolve($onFulfilledOrRejected())->then(function() use($value) {
133 3
                    return $value;
134 4
                });
135 5
            }
136
        );
137
    }
138
139
    /**
140
     * @override
141
     * @inheritDoc
142
     */
143 1
    public function isPending()
144
    {
145 1
        return false;
146
    }
147
148
    /**
149
     * @override
150
     * @inheritDoc
151
     */
152 3
    public function isFulfilled()
153
    {
154 3
        return true;
155
    }
156
157
    /**
158
     * @override
159
     * @inheritDoc
160
     */
161 3
    public function isRejected()
162
    {
163 3
        return false;
164
    }
165
166
    /**
167
     * @override
168
     * @inheritDoc
169
     */
170 3
    public function isCancelled()
171
    {
172 3
        return false;
173
    }
174
175
    /**
176
     * @override
177
     * @inheritDoc
178
     */
179
    public function getPromise()
180
    {
181
        return $this;
182
    }
183
184
    /**
185
     * @override
186
     * @inheritDoc
187
     */
188
    public function resolve($value = null)
189
    {
190
        return $this;
191
    }
192
193
    /**
194
     * @override
195
     * @inheritDoc
196
     */
197
    public function reject($reason = null)
198
    {
199
        return $this;
200
    }
201
202
    /**
203
     * @override
204
     * @inheritDoc
205
     */
206 7
    public function cancel($reason = null)
207
    {
208 7
        return $this;
209
    }
210
211
    /**
212
     * @see Promise::getValue
213
     */
214 260
    protected function getValue()
215
    {
216 260
        return $this->value;
217
    }
218
219
    /**
220
     * @see Promise::getReason
221
     */
222
    protected function getReason()
223
    {
224
        return null;
225
    }
226
}
227