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

RejectedPromise   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A then() 0 8 2
A done() 0 6 2
A state() 0 4 1
A rejectActual() 0 10 2
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