PromiseCancelled::done()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 8

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 3
crap 4.0218
1
<?php
2
3
namespace Dazzle\Promise;
4
5
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException;
6
use Dazzle\Throwable\Exception\Runtime\CancellationException;
7
use Error;
8
use Exception;
9
10
class PromiseCancelled implements PromiseInterface
11
{
12
    /**
13
     * @var Error|Exception|string|null
14
     */
15
    protected $reason;
16
17
    /**
18
     * @param Error|Exception|string|null $reason
19
     * @throws InvalidArgumentException
20
     */
21 140
    public function __construct($reason = null)
22
    {
23 140
        if ($reason instanceof PromiseInterface)
24
        {
25 1
            throw new InvalidArgumentException(
26 1
                'You cannot create PromiseCancelled with a promise. Use Promise::doCancel($promiseOrValue) instead.'
27
            );
28
        }
29
30 140
        $this->reason = $reason;
31 140
    }
32
33
    /**
34
     *
35
     */
36 134
    public function __destruct()
37
    {
38 134
        unset($this->reason);
39 134
    }
40
41
    /**
42
     * @override
43
     * @inheritDoc
44
     */
45 82
    public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null)
46
    {
47 82
        if (null === $onCancel)
48
        {
49 31
            return $this;
50
        }
51
52
        try
53
        {
54 81
            return Promise::doResolve($onCancel($this->getReason()))
55 81
                ->then(
56
                    function() {
57 81
                        return Promise::doCancel($this->getReason());
58 81
                    },
59
                    function() {
60 6
                        return Promise::doCancel($this->getReason());
61 81
                    }
62
                );
63
        }
64 5
        catch (Error $ex)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
65
        {}
66 5
        catch (Exception $ex)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
67
        {}
68
69 5
        return Promise::doCancel($this->getReason());
70
    }
71
72
    /**
73
     * @override
74
     * @inheritDoc
75
     */
76 65 View Code Duplication
    public function done(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...
77
    {
78 65
        if (null === $onCancel)
79
        {
80 6
            $this->throwError($this->getReason());
0 ignored issues
show
Bug introduced by
It seems like $this->getReason() targeting Dazzle\Promise\PromiseCancelled::getReason() can also be of type null; however, Dazzle\Promise\PromiseCancelled::throwError() does only seem to accept object<Exception>|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
81
        }
82
83 59
        $result = $onCancel($this->getReason());
84
85 54
        if ($result instanceof self)
86
        {
87 11
            $this->throwError($result->getReason());
0 ignored issues
show
Bug introduced by
It seems like $result->getReason() targeting Dazzle\Promise\PromiseCancelled::getReason() can also be of type null; however, Dazzle\Promise\PromiseCancelled::throwError() does only seem to accept object<Exception>|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
88
        }
89
90 43
        if ($result instanceof PromiseInterface)
91
        {
92
            $result->done();
93
        }
94 43
    }
95
96
    /**
97
     * @override
98
     * @inheritDoc
99
     */
100 2
    public function spread(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null)
101
    {
102 2
        return $this->then(
103 2
            null,
104 2
            null,
105
            function($reasons) use($onCancel) {
106 2
                return $onCancel(...((array) $reasons));
107 2
            }
108
        );
109
    }
110
111
    /**
112
     * @override
113
     * @inheritDoc
114
     */
115 1
    public function success(callable $onSuccess)
116
    {
117 1
        return $this->then($onSuccess);
118
    }
119
120
    /**
121
     * @override
122
     * @inheritDoc
123
     */
124 1
    public function failure(callable $onFailure)
125
    {
126 1
        return $this->then(null, $onFailure);
127
    }
128
129
    /**
130
     * @override
131
     * @inheritDoc
132
     */
133 1
    public function abort(callable $onCancel)
134
    {
135 1
        return $this->then(null, null, $onCancel);
136
    }
137
138
    /**
139
     * @override
140
     * @inheritDoc
141
     */
142 5 View Code Duplication
    public function always(callable $onFulfilledOrRejected)
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...
143
    {
144 5
        return $this->then(
145 5
            null,
146 5
            null,
147
            function($reason) use($onFulfilledOrRejected) {
148 4
                return Promise::doResolve($onFulfilledOrRejected())->then(function() use($reason) {
149 3
                    return new static($reason);
150 4
                });
151 5
            }
152
        );
153
    }
154
155
    /**
156
     * @override
157
     * @inheritDoc
158
     */
159 1
    public function isPending()
160
    {
161 1
        return false;
162
    }
163
164
    /**
165
     * @override
166
     * @inheritDoc
167
     */
168 3
    public function isFulfilled()
169
    {
170 3
        return false;
171
    }
172
173
    /**
174
     * @override
175
     * @inheritDoc
176
     */
177 3
    public function isRejected()
178
    {
179 3
        return false;
180
    }
181
182
    /**
183
     * @override
184
     * @inheritDoc
185
     */
186 5
    public function isCancelled()
187
    {
188 5
        return true;
189
    }
190
191
    /**
192
     * @override
193
     * @inheritDoc
194
     */
195
    public function getPromise()
196
    {
197
        return $this;
198
    }
199
200
    /**
201
     * @override
202
     * @inheritDoc
203
     */
204 1
    public function resolve($value = null)
205
    {
206 1
        return $this;
207
    }
208
209
    /**
210
     * @override
211
     * @inheritDoc
212
     */
213 1
    public function reject($reason = null)
214
    {
215 1
        return $this;
216
    }
217
218
    /**
219
     * @override
220
     * @inheritDoc
221
     */
222 1
    public function cancel($reason = null)
223
    {
224 1
        return $this;
225
    }
226
227
    /**
228
     * @see Promise::getValue
229
     */
230
    protected function getValue()
231
    {
232
        return null;
233
    }
234
235
    /**
236
     * @see Promise::getReason
237
     */
238 112
    protected function getReason()
239
    {
240 112
        return $this->reason;
241
    }
242
243
    /**
244
     * @param Error|Exception|string $reason
245
     * @throws Error|Exception
246
     */
247 17
    protected function throwError($reason)
248
    {
249 17
        if ($reason instanceof Error || $reason instanceof Exception)
250
        {
251 6
            throw $reason;
252
        }
253
254 11
        throw new CancellationException($reason);
255
    }
256
}
257