Completed
Branch feature/pre-split (9d6b17)
by Anton
03:28
created

AbstractCommand::onRollBack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Commands;
8
9
use Spiral\ORM\CommandInterface;
10
11
/**
12
 * Provides support for command events.
13
 */
14
abstract class AbstractCommand implements CommandInterface
15
{
16
    /**
17
     * @var \Closure
18
     */
19
    private $onExecute = [];
20
    private $onComplete = [];
21
    private $onRollBack = [];
22
23
    /**
24
     * Closure to be called after command executing.
25
     *
26
     * @param \Closure $closure
27
     */
28
    final public function onExecute(\Closure $closure)
29
    {
30
        $this->onExecute[] = $closure;
31
    }
32
33
    /**
34
     * To be called after parent transaction been commited.
35
     *
36
     * @param \Closure $closure
37
     */
38
    final public function onComplete(\Closure $closure)
39
    {
40
        $this->onComplete[] = $closure;
41
    }
42
43
    /**
44
     * To be called after parent transaction been rolled back.
45
     *
46
     * @param \Closure $closure
47
     */
48
    final public function onRollBack(\Closure $closure)
49
    {
50
        $this->onRollBack[] = $closure;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function execute()
57
    {
58
        foreach ($this->onExecute as $closure) {
0 ignored issues
show
Bug introduced by
The expression $this->onExecute of type object<Closure> is not traversable.
Loading history...
59
            call_user_func($closure, $this);
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function complete()
67
    {
68
        foreach ($this->onComplete as $closure) {
69
            call_user_func($closure, $this);
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function rollBack()
77
    {
78
        foreach ($this->onRollBack as $closure) {
79
            call_user_func($closure, $this);
80
        }
81
    }
82
}