ObservableResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 96
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A reject() 0 4 1
A onResolve() 0 4 1
A onNotify() 0 4 1
A callResolveCallback() 0 4 1
A callRejectCallback() 0 4 1
A callNotifyCallback() 0 4 1
A invokeCallback() 0 4 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
/**
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