Completed
Push — master ( cd637e...97db54 )
by Thomas
02:44
created

Operation::onRollback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
     * @var string[]
14
     */
15
    protected $idx;
16
17
    /**
18
     * @var callable
19
     */
20
    protected $commit;
21
22
    /**
23
     * @var callable
24
     */
25
    protected $rollback;
26
27
    /**
28
     * @var mixed
29
     */
30
    protected $value;
31
32
    /**
33
     * @var mixed
34
     */
35
    protected $result;
36
37
    /**
38
     * Set commit callback.
39
     *
40
     * @param callable $callback
41
     *   The callback when this operation is committed.
42
     *
43
     * @return $this
44
     */
45 6
    public function onCommit(callable $callback)
46
    {
47 6
        $this->commit = $callback;
48 6
        return $this;
49
    }
50
51
    /**
52
     * Set rollback callback.
53
     *
54
     * @param callable $callback
55
     *   The callback when this operation is rolled back.
56
     *
57
     * @return $this
58
     */
59 4
    public function onRollback(callable $callback)
60
    {
61 4
        $this->rollback = $callback;
62 4
        return $this;
63
    }
64
65
    /**
66
     * Get commit callback.
67
     *
68
     * @return callable|null
69
     */
70 1
    public function getCommitCallback()
71
    {
72 1
        return $this->commit;
73
    }
74
75
    /**
76
     * Get rollback callback.
77
     *
78
     * @return callable|null
79
     */
80 1
    public function getRollbackCallback()
81
    {
82 1
        return $this->rollback;
83
    }
84
85
    /**
86
     * @param mixed $value
87
     *   The value to set.
88
     *
89
     * @return $this
90
     */
91 1
    public function setValue($value)
92
    {
93 1
        $this->value = $value;
94 1
        return $this;
95
    }
96
97
    /**
98
     * Get value.
99
     *
100
     * @return mixed
101
     */
102 1
    public function getValue()
103
    {
104 1
        return is_callable($this->value) ? call_user_func($this->value) : $this->value;
105
    }
106
107
    /**
108
     * Get result from callback.
109
     *
110
     * @return mixed
111
     */
112 4
    public function getResult()
113
    {
114 4
        return $this->result;
115
    }
116
117
    /**
118
     * @param Connection $connection
119
     *   The connection to use for this id.
120
     * @param string $idx
121
     *   The id.
122
     *
123
     * @return $this
124
     */
125 7
    public function setIdx(Connection $connection, $idx)
126
    {
127 7
        $this->idx[$connection->connectionId()] = $idx;
128 7
        return $this;
129
    }
130
131
    /**
132
     * Get id.
133
     *
134
     * @param Connection $connection
135
     *   The connection to get id from.
136
     *
137
     * @return string|null
138
     */
139 7
    public function idx(Connection $connection)
140
    {
141 7
        $connectionId = $connection->connectionId();
142 7
        return isset($this->idx[$connectionId]) ? $this->idx[$connectionId] : null;
143
    }
144
145
    /**
146
     * Execute commit operation.
147
     *
148
     * @return mixed
149
     */
150 5
    public function commit()
151
    {
152 5
        return $this->result = $this->commit ? call_user_func($this->commit) : NULL;
153
    }
154
155
    /**
156
     * Execute rollback operation.
157
     *
158
     * @return mixed
159
     */
160 3
    public function rollback()
161
    {
162 3
        return $this->result = $this->rollback ? call_user_func($this->rollback) : NULL;
163
    }
164
}
165