Completed
Push — master ( 9672ef...b72795 )
by Ivannis Suárez
02:30
created

RejectedPromise::done()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
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
 * Rejected Promise class.
16
 *
17
 * @author Karel Osorio Ramírez <[email protected]>
18
 */
19
class RejectedPromise extends AbstractPromise
20
{
21
    /**
22
     * @var mixed
23
     */
24
    protected $reason;
25
26
    /**
27
     * @param mixed $reason
28
     */
29
    public function __construct($reason = null)
30
    {
31
        $this->reason = $reason;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null)
38
    {
39
        if ($onRejected === null) {
40
            return $this;
41
        }
42
43
        return new self($this->rejectActual($onRejected));
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null)
50
    {
51
        if ($onRejected !== null) {
52
            $onRejected($this->reason);
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function state()
60
    {
61
        return State::REJECTED();
62
    }
63
64
    /**
65
     * @param callable $onRejected
66
     *
67
     * @return mixed
68
     */
69
    private function rejectActual(callable $onRejected)
70
    {
71
        try {
72
            $onRejected($this->reason);
73
        } catch (\Exception $e) {
74
            return $e;
75
        }
76
77
        return $this->reason;
78
    }
79
}
80