Deferred::notify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Thruster\Component\Promise;
4
5
/**
6
 * Class Deferred
7
 *
8
 * @package Thruster\Component\Promise
9
 * @author  Aurimas Niekis <[email protected]>
10
 */
11
class Deferred implements PromisorInterface
12
{
13
    /**
14
     * @var Promise
15
     */
16
    private $promise;
17
18
    /**
19
     * @var callable
20
     */
21
    private $resolveCallback;
22
23
    /**
24
     * @var callable
25
     */
26
    private $rejectCallback;
27
28
    /**
29
     * @var callable
30
     */
31
    private $notifyCallback;
32
33
    /**
34
     * @var callable
35
     */
36
    private $canceller;
37
38 242
    public function __construct(callable $canceller = null)
39
    {
40 242
        $this->canceller = $canceller;
41 242
    }
42
43 242
    public function promise() : PromiseInterface
44
    {
45 242
        if (null === $this->promise) {
46 242
            $this->promise = new Promise(function ($resolve, $reject, $notify) {
47 242
                $this->resolveCallback = $resolve;
48 242
                $this->rejectCallback  = $reject;
49 242
                $this->notifyCallback  = $notify;
50 242
            }, $this->canceller);
51
        }
52
53 242
        return $this->promise;
54
    }
55
56 100
    public function resolve($value = null)
57
    {
58 100
        $this->promise();
59
60 100
        call_user_func($this->resolveCallback, $value);
61 96
    }
62
63 94
    public function reject($reason = null)
64
    {
65 94
        $this->promise();
66
67 94
        call_user_func($this->rejectCallback, $reason);
68 83
    }
69
70 29
    public function notify($update = null)
71
    {
72 29
        $this->promise();
73
74 29
        call_user_func($this->notifyCallback, $update);
75 27
    }
76
}
77