Completed
Push — master ( 298e09...565401 )
by Ivannis Suárez
02:36
created

FulfilledPromise::state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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