Transaction::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Dazzle\MySQL;
4
5
use Dazzle\Event\BaseEventEmitter;
6
use Dazzle\MySQL\Protocol\Command\QueryCommand;
7
use Dazzle\MySQL\Protocol\CommandInterface;
8
use Dazzle\MySQL\Protocol\Query;
9
use Dazzle\MySQL\Protocol\QueryInterface;
10
use Dazzle\Promise\Promise;
11
use Dazzle\Promise\PromiseInterface;
12
use Dazzle\Throwable\Exception\Runtime\ExecutionException;
13
use SplQueue;
14
15
class Transaction extends BaseEventEmitter implements TransactionInterface
16
{
17
    /**
18
     * @var DatabaseInterface
19
     */
20
    protected $database;
21
22
    /**
23
     * @var CommandInterface[]
24
     */
25
    protected $queue;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $open;
31
32
    /**
33
     * @param DatabaseInterface $database
34
     */
35
    public function __construct(DatabaseInterface $database)
36
    {
37
        $this->database = $database;
38
        $this->queue = [];
39
        $this->open = true;
40
    }
41
42
    /**
43
     * @override
44
     * @inheritDoc
45
     */
46
    public function isOpen()
47
    {
48
        return $this->open;
49
    }
50
51
    /**
52
     * @override
53
     * @inheritDoc
54
     */
55
    public function query($sql, $sqlParams = [])
56
    {
57
        if (!$this->isOpen())
58
        {
59
            return Promise::doReject(new ExecutionException('This transaction is no longer open.'));
60
        }
61
62
        $promise = new Promise();
63
        $query   = new Query($sql, $sqlParams);
64
        $command = new QueryCommand($this->database, $query);
65
66
        $this->on('error', function ($trans, $err) use ($promise) {
67
            return $promise->reject($err);
68
        });
69
        $this->on('success', function ($trans) use ($promise, $command) {
0 ignored issues
show
Unused Code introduced by
The parameter $trans is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
            return $promise->resolve($command);
71
        });
72
73
        $this->queue[] = $command;
74
75
        return $promise;
76
    }
77
78
    /**
79
     * @override
80
     * @inheritDoc
81
     */
82
    public function execute($sql, $sqlParams = [])
83
    {
84
        return $this->query($sql, $sqlParams)->then(function($command) {
85
            return $command->affectedRows;
86
        });
87
    }
88
89
    /**
90
     * @override
91
     * @inheritDoc
92
     */
93
    public function commit()
94
    {
95
        if (!$this->isOpen())
96
        {
97
            return Promise::doReject(new ExecutionException('This transaction is no longer open.'));
98
        }
99
100
        $promise = new Promise();
101
102
        $this->on('error', function ($trans, $err) use ($promise) {
103
            return $promise->reject($err);
104
        });
105
        $this->on('success', function ($trans) use ($promise) {
0 ignored issues
show
Unused Code introduced by
The parameter $trans is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
            return $promise->resolve();
107
        });
108
109
        $this->open = false;
110
        $this->emit('commit', [ $this, $this->queue ]);
111
        $this->queue = [];
112
113
        return $promise;
114
    }
115
116
    /**
117
     * @override
118
     * @inheritDoc
119
     */
120
    public function rollback()
121
    {
122
        if (!$this->isOpen())
123
        {
124
            return Promise::doReject(new ExecutionException('This transaction is no longer open.'));
125
        }
126
127
        $this->open = false;
128
        $this->emit('rollback', [ $this ]);
129
        $this->queue = [];
130
131
        return Promise::doResolve();
132
    }
133
}
134