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

Deferred   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A promise() 0 18 2
A resolve() 0 6 1
A reject() 0 6 1
A notify() 0 6 1
A cancel() 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
use Cubiche\Core\Delegate\Delegate;
15
16
/**
17
 * Deferred class.
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 */
21
class Deferred implements DeferredInterface
22
{
23
    /**
24
     * @var PromiseInterface
25
     */
26
    protected $promise;
27
28
    /**
29
     * @var Delegate
30
     */
31
    protected $resolveDelegate;
32
33
    /**
34
     * @var Delegate
35
     */
36
    protected $rejectDelegate;
37
38
    /**
39
     * @var Delegate
40
     */
41
    protected $notifyDelegate;
42
43
    /**
44
     * Constructor.
45
     */
46
    public function __construct()
47
    {
48
        $this->promise = null;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function promise()
55
    {
56
        if ($this->promise === null) {
57
            $this->promise = new Promise(
58
                new Delegate(function (callable $callable) {
59
                    $this->resolveDelegate = new Delegate($callable);
60
                }),
61
                new Delegate(function (callable $callable) {
62
                    $this->rejectDelegate = new Delegate($callable);
63
                }),
64
                new Delegate(function (callable $callable) {
65
                    $this->notifyDelegate = new Delegate($callable);
66
                })
67
            );
68
        }
69
70
        return $this->promise;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function resolve($value = null)
77
    {
78
        $this->promise();
79
80
        $this->resolveDelegate->__invoke($value);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function reject($reason = null)
87
    {
88
        $this->promise();
89
90
        $this->rejectDelegate->__invoke($reason);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function notify($state = null)
97
    {
98
        $this->promise();
99
100
        $this->notifyDelegate->__invoke($state);
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function cancel()
107
    {
108
        if ($this->promise()->state()->equals(State::PENDING())) {
109
            $this->reject(new CancellationException());
110
111
            return true;
112
        }
113
114
        return false;
115
    }
116
}
117