Deferred::reject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dazzle\Promise;
4
5
use Error;
6
use Exception;
7
8
class Deferred implements DeferredInterface
9
{
10
    /**
11
     * @var PromiseInterface|null
12
     */
13
    protected $promise;
14
15
    /**
16
     * @var callable
17
     */
18
    protected $resolveCallback;
19
20
    /**
21
     * @var callable
22
     */
23
    protected $rejectCallback;
24
25
    /**
26
     * @var callable
27
     */
28
    protected $cancelCallback;
29
30
    /**
31
     * @var callable|null
32
     */
33
    protected $canceller;
34
35
    /**
36
     * @param callable $canceller
37
     */
38 167
    public function __construct($canceller = null)
39
    {
40 167
        $this->promise = null;
41
        $this->resolveCallback = function($value = null) {};
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
        $this->rejectCallback = function($reason = null) {};
0 ignored issues
show
Unused Code introduced by
The parameter $reason is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
        $this->cancelCallback = function($reason = null) {};
0 ignored issues
show
Unused Code introduced by
The parameter $reason is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44 167
        $this->canceller = $canceller;
45 167
    }
46
47
    /**
48
     *
49
     */
50 167
    public function __destruct()
51
    {
52 167
        unset($this->promise);
53 167
        unset($this->resolveCallback);
54 167
        unset($this->rejectCallback);
55 167
        unset($this->cancelCallback);
56 167
        unset($this->canceller);
57 167
    }
58
59
    /**
60
     * @override
61
     * @inheritDoc
62
     */
63 167
    public function getPromise()
64
    {
65 167
        if (null === $this->promise)
66
        {
67 167
            $this->promise = new Promise(function($resolve, $reject, $cancel) {
68 167
                $this->resolveCallback = $resolve;
69 167
                $this->rejectCallback  = $reject;
70 167
                $this->cancelCallback  = $cancel;
71 167
            }, $this->canceller);
72
        }
73
74 167
        return $this->promise;
75
    }
76
77
    /**
78
     * @override
79
     * @inheritDoc
80
     */
81 52
    public function resolve($value = null)
82
    {
83 52
        $this->getPromise();
84
85 52
        return call_user_func($this->resolveCallback, $value);
86
    }
87
88
    /**
89
     * @override
90
     * @inheritDoc
91
     */
92 50
    public function reject($reason = null)
93
    {
94 50
        $this->getPromise();
95
96 50
        return call_user_func($this->rejectCallback, $reason);
97
    }
98
99
    /**
100
     * @override
101
     * @inheritDoc
102
     */
103 41
    public function cancel($reason = null)
104
    {
105 41
        $this->getPromise();
106
107 41
        return call_user_func($this->cancelCallback, $reason);
108
    }
109
}
110