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

Promise   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 70
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A then() 0 4 1
A state() 0 4 1
A resolve() 0 4 1
A reject() 0 4 1
A notify() 0 4 1
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
 * Promise class.
16
 *
17
 * @author Karel Osorio Ramírez <[email protected]>
18
 */
19
class Promise extends AbstractPromise
20
{
21
    /**
22
     * @var DeferredInterface
23
     */
24
    private $deferred;
25
26
    /**
27
     * @param callable $exportResolve
28
     * @param callable $exportReject
29
     * @param callable $exportNotify
30
     */
31
    public function __construct(
32
        callable $exportResolve,
33
        callable $exportReject,
34
        callable $exportNotify
35
    ) {
36
        $this->deferred = new PromiseDeferred();
37
38
        $exportResolve(function ($value = null) {
39
            return $this->resolve($value);
40
        });
41
        $exportReject(function ($reason = null) {
42
            return $this->reject($reason);
43
        });
44
        $exportNotify(function ($state = null) {
45
            return $this->notify($state);
46
        });
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null)
53
    {
54
        return $this->deferred->promise()->then($onFulfilled, $onRejected, $onNotify);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function state()
61
    {
62
        return $this->deferred->promise()->state();
63
    }
64
65
    /**
66
     * @param mixed $value
67
     */
68
    protected function resolve($value = null)
69
    {
70
        $this->deferred->resolve($value);
71
    }
72
73
    /**
74
     * @param mixed $reason
75
     */
76
    protected function reject($reason = null)
77
    {
78
        $this->deferred->reject($reason);
79
    }
80
81
    /**
82
     * @param mixed $state
83
     */
84
    protected function notify($state = null)
85
    {
86
        $this->deferred->notify($state);
87
    }
88
}
89