Completed
Branch feature/pre-split (9269d3)
by Anton
05:27
created

AbstractCommand::complete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
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 = null;
20
    private $onComplete = null;
21
    private $onRollback = null;
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
        if (!empty($this->onExecute)) {
59
            call_user_func($this->onExecute, $this);
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function complete()
67
    {
68
        if (!empty($this->onComplete)) {
69
            call_user_func($this->onComplete, $this);
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function rollBack()
77
    {
78
        if (!empty($this->onRollback)) {
79
            call_user_func($this->onRollback, $this);
80
        }
81
    }
82
}