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

FulfilledPromise   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A then() 0 12 3
A state() 0 4 1
A resolveActual() 0 6 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
 * 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