RequestRecord::getTimeout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dazzle\Channel\Record;
4
5
use Dazzle\Throwable\ThrowableProxy;
6
use Error;
7
use Exception;
8
9
class RequestRecord
10
{
11
    /**
12
     * @var string
13
     */
14
    public $pid;
15
16
    /**
17
     * @var callable
18
     */
19
    public $success;
20
21
    /**
22
     * @var callable
23
     */
24
    public $failure;
25
26
    /**
27
     * @var callable
28
     */
29
    public $cancel;
30
31
    /**
32
     * @var float
33
     */
34
    public $timeout;
35
36
    /**
37
     * @param string $pid
38
     * @param callable|null $success
39
     * @param callable|null $failure
40
     * @param callable|null $cancel
41
     * @param float $timeout
42
     */
43 18
    public function __construct($pid, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0)
44
    {
45 18
        $this->pid     = $pid;
46
        $this->success = ($success !== null) ? $success : function() {};
47
        $this->failure = ($failure !== null) ? $failure : function() {};
48
        $this->cancel  = ($cancel !== null)  ? $cancel  : function() {};
49 18
        $this->timeout = $timeout;
50 18
    }
51
52
    /**
53
     *
54
     */
55 1
    public function __destruct()
56
    {
57 1
        unset($this->pid);
58 1
        unset($this->success);
59 1
        unset($this->failure);
60 1
        unset($this->cancel);
61 1
        unset($this->timeout);
62 1
    }
63
64
    /**
65
     * Return protocol ID.
66
     *
67
     * @return string
68
     */
69 2
    public function getPid()
70
    {
71 2
        return $this->pid;
72
    }
73
74
    /**
75
     * Return success handler.
76
     *
77
     * @return callable
78
     */
79 4
    public function onSuccess()
80
    {
81 4
        return $this->success;
82
    }
83
84
    /**
85
     * Return failure handler.
86
     *
87
     * @return callable
88
     */
89 4
    public function onFailure()
90
    {
91 4
        return $this->failure;
92
    }
93
94
    /**
95
     * Return cancellation handler.
96
     *
97
     * @return callable
98
     */
99 4
    public function onCancel()
100
    {
101 4
        return $this->cancel;
102
    }
103
104
    /**
105
     * Return timeout.
106
     *
107
     * @return float
108
     */
109 3
    public function getTimeout()
110
    {
111 3
        return $this->timeout;
112
    }
113
114
    /**
115
     * Resolve Request calling success handler.
116
     *
117
     * @param mixed $value
118
     * @return mixed
119
     */
120 1
    public function resolve($value)
121
    {
122 1
        $callback = $this->onSuccess();
123 1
        return $callback($value);
124
    }
125
126
    /**
127
     * Reject Request calling failure handler.
128
     *
129
     * @param Error|Exception|ThrowableProxy $ex
130
     * @return mixed
131
     */
132 1
    public function reject($ex)
133
    {
134 1
        $callback = $this->onFailure();
135 1
        return $callback($ex);
136
    }
137
138
    /**
139
     * Cancel Request calling cancellation handler.
140
     *
141
     * @param Error|Exception|ThrowableProxy $ex
142
     * @return mixed
143
     */
144 1
    public function cancel($ex)
145
    {
146 1
        $callback = $this->onCancel();
147 1
        return $callback($ex);
148
    }
149
}
150