Completed
Push — master ( 4e3eed...0c868b )
by Thomas
02:41
created

Operation::getRollbackCallbacks()   A

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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Gielfeldt\TransactionalPHP;
4
5
/**
6
 * Class Operation
7
 *
8
 * @package Gielfeldt\TransactionalPHP
9
 */
10
class Operation
11
{
12
    /**
13
     * The index in the buffer, keyed by connection id.
14
     *
15
     * @var string[]
16
     */
17
    protected $idx;
18
19
    /**
20
     * Callback for commit.
21
     *
22
     * @var callable
23
     */
24
    protected $commit = [];
25
26
    /**
27
     * Callback for rollback.
28
     *
29
     * @var callable
30
     */
31
    protected $rollback = [];
32
33
    /**
34
     * Callback for buffer.
35
     *
36
     * @var callable
37
     */
38
    protected $buffer = [];
39
40
    /**
41
     * Value for operation.
42
     *
43
     * @var mixed
44
     */
45
    protected $value;
46
47
    /**
48
     * Result of callback execution.
49
     *
50
     * @var mixed
51
     */
52
    protected $result;
53
54
    /**
55
     * Set commit callback.
56
     *
57
     * @param callable $callback
58
     *   The callback when this operation is committed.
59
     *
60
     * @return $this
61
     */
62 6
    public function onCommit(callable $callback)
63
    {
64 6
        $this->commit[] = $callback;
65 6
        return $this;
66
    }
67
68
    /**
69
     * Set rollback callback.
70
     *
71
     * @param callable $callback
72
     *   The callback when this operation is rolled back.
73
     *
74
     * @return $this
75
     */
76 5
    public function onRollback(callable $callback)
77
    {
78 5
        $this->rollback[] = $callback;
79 5
        return $this;
80
    }
81
82
    /**
83
     * Set buffer callback.
84
     *
85
     * @param callable $callback
86
     *   The callback when this operation is buffered.
87
     *
88
     * @return $this
89
     */
90 1
    public function onBuffer(callable $callback)
91
    {
92 1
        $this->buffer[] = $callback;
93 1
        return $this;
94
    }
95
96
    /**
97
     * Get commit callback.
98
     *
99
     * @return callable|null
100
     */
101 1
    public function getCommitCallbacks()
102
    {
103 1
        return $this->commit;
104
    }
105
106
    /**
107
     * Get rollback callback.
108
     *
109
     * @return callable|null
110
     */
111 1
    public function getRollbackCallbacks()
112
    {
113 1
        return $this->rollback;
114
    }
115
116
    /**
117
     * Get buffer callback.
118
     *
119
     * @return callable|null
120
     */
121 1
    public function getBufferCallbacks()
122
    {
123 1
        return $this->buffer;
124
    }
125
126
    /**
127
     * @param mixed $value
128
     *   The value to set.
129
     *
130
     * @return $this
131
     */
132 2
    public function setValue($value)
133
    {
134 2
        $this->value = $value;
135 2
        return $this;
136
    }
137
138
    /**
139
     * Get value.
140
     *
141
     * @return mixed
142
     */
143 2
    public function getValue()
144
    {
145 2
        return is_callable($this->value) ? call_user_func($this->value) : $this->value;
146
    }
147
148
    /**
149
     * Get result from callback.
150
     *
151
     * @return mixed
152
     */
153 5
    public function getResult()
154
    {
155 5
        return $this->result;
156
    }
157
158
    /**
159
     * @param Connection $connection
160
     *   The connection to use for this id.
161
     * @param string $idx
162
     *   The id.
163
     *
164
     * @return $this
165
     */
166 8
    public function setIdx(Connection $connection, $idx)
167
    {
168 8
        $this->idx[$connection->connectionId()] = $idx;
169 8
        return $this;
170
    }
171
172
    /**
173
     * Get id.
174
     *
175
     * @param Connection $connection
176
     *   The connection to get id from.
177
     *
178
     * @return string|null
179
     */
180 8
    public function idx(Connection $connection)
181
    {
182 8
        $connectionId = $connection->connectionId();
183 8
        return isset($this->idx[$connectionId]) ? $this->idx[$connectionId] : null;
184
    }
185
186
    /**
187
     * Execute commit operation.
188
     *
189
     * @param Connection $connection
190
     *   The connection to run this operation on.
191
     *
192
     * @return mixed
193
     */
194 6
    public function commit($connection = null)
195
    {
196 6
        foreach ($this->commit as $callback) {
0 ignored issues
show
Bug introduced by
The expression $this->commit of type callable is not traversable.
Loading history...
197 4
            $this->result = call_user_func($callback, $this, $connection);
198 6
        }
199 6
        return $this->result;
200
    }
201
202
    /**
203
     * Execute rollback operation.
204
     *
205
     * @param Connection $connection
206
     *   The connection to run this operation on.
207
     *
208
     * @return mixed
209
     */
210 4
    public function rollback($connection = null)
211
    {
212 4
        foreach ($this->rollback as $callback) {
0 ignored issues
show
Bug introduced by
The expression $this->rollback of type callable is not traversable.
Loading history...
213 4
            $this->result = call_user_func($callback, $this, $connection);
214 4
        }
215 4
        return $this->result;
216
    }
217
218
    /**
219
     * Execute buffer operation.
220
     *
221
     * @param Connection $connection
222
     *   The connection to run this operation on.
223
     *
224
     * @return mixed
225
     */
226 8
    public function buffer($connection = null)
227
    {
228 8
        foreach ($this->buffer as $callback) {
0 ignored issues
show
Bug introduced by
The expression $this->buffer of type callable is not traversable.
Loading history...
229 1
            $this->result = call_user_func($callback, $this, $connection);
230 8
        }
231 8
        return $this->result;
232
    }
233
}
234