Completed
Push — master ( 9672ef...b72795 )
by Ivannis Suárez
02:30
created

ObservableResolver::onNotify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
 * Observable Resolver class.
16
 *
17
 * @author Karel Osorio Ramírez <[email protected]>
18
 */
19
class ObservableResolver extends Resolver
20
{
21
    /**
22
     * @var callable
23
     */
24
    protected $resolveCallback;
25
26
    /**
27
     * @var callable
28
     */
29
    protected $rejectCallback;
30
31
    /**
32
     * @var callable
33
     */
34
    protected $notifyCallback;
35
36
    /**
37
     * @param callable $resolveCallback
38
     * @param callable $rejectCallback
39
     * @param callable $notifyCallback
40
     */
41
    public function __construct(
42
        callable $resolveCallback = null,
43
        callable $rejectCallback = null,
44
        callable $notifyCallback = null
45
    ) {
46
        $this->resolveCallback = $resolveCallback;
47
        $this->rejectCallback = $rejectCallback;
48
        $this->notifyCallback = $notifyCallback;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function reject($reason = null)
55
    {
56
        $this->callRejectCallback($reason);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function onResolve($value = null)
63
    {
64
        $this->callResolveCallback($value);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function onNotify($state = null)
71
    {
72
        $this->callNotifyCallback($state);
73
    }
74
75
    /**
76
     * @param mixed $value
77
     *
78
     * @return mixed
79
     */
80
    protected function callResolveCallback($value = null)
81
    {
82
        return $this->invokeCallback($this->resolveCallback, $value);
83
    }
84
85
    /**
86
     * @param mixed $reason
87
     *                      return mixed
88
     */
89
    protected function callRejectCallback($reason = null)
90
    {
91
        return $this->invokeCallback($this->rejectCallback, $reason);
92
    }
93
94
    /**
95
     * @param mixed $state
96
     *
97
     * @return mixed
98
     */
99
    protected function callNotifyCallback($state = null)
100
    {
101
        return $this->invokeCallback($this->notifyCallback, $state);
102
    }
103
104
    /**
105
     * @param callable $callback
106
     * @param mixed    $argument
107
     *
108
     * @return mixed
109
     */
110
    private function invokeCallback(callable $callback = null, $argument = null)
111
    {
112
        return $callback === null ? $argument : \call_user_func($callback, $argument);
113
    }
114
}
115