Completed
Pull Request — master (#1)
by
unknown
08:32
created

Transaction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
namespace Dazzle\PgSQL\Transaction;
3
4
use Dazzle\Event\BaseEventEmitter;
5
use Dazzle\Event\EventEmitterInterface;
6
use Dazzle\PgSQL\Result\CommandResultStatement;
7
use Dazzle\Promise\Promise;
8
use Dazzle\PgSQL\Connection\ConnectorInterface;
9
10
class Transaction extends BaseEventEmitter implements TransactionInterface
11
{
12
    /**
13
     * @var ConnectorInterface
14
     */
15
    protected $connector;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $open;
21
22
    /**
23
     * @param ConnectorInterface $connector
24
     * @param EventEmitterInterface $emitter
25
     */
26
    public function __construct(ConnectorInterface $connector, EventEmitterInterface $emitter)
27
    {
28
        $this->connector = $connector;
29
        $this->emitter = $emitter;
0 ignored issues
show
Bug introduced by
The property emitter does not seem to exist. Did you mean emitterBlocked?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
30
        $this->open = false;
31
    }
32
33
    /**
34
     * @override
35
     * @inheritDoc
36
     */
37
    public function isOpen()
38
    {
39
        return $this->open;
40
    }
41
42
    /**
43
     * @override
44
     * @inheritDoc
45
     */
46
    public function query($sql, array $sqlParams = [])
47
    {
48
        return $this->connector->query($sql, $sqlParams);
49
    }
50
51
    /**
52
     * @override
53
     * @inheritDoc
54
     */
55
    public function execute($sql, array $sqlParams = [])
56
    {
57
        return $this->connector->execute($sql, $sqlParams);
58
    }
59
60
    public function begin()
61
    {
62
        if ($this->isOpen()) {
63
            return Promise::doResolve($this);
64
        }
65
66
        return $this->connector->execute('BEGIN')->success(function (CommandResultStatement $result) {
67
            if ($result) {
68
                $this->emitter->emit('transaction:begin');
0 ignored issues
show
Bug introduced by
The property emitter does not seem to exist. Did you mean emitterBlocked?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
            }
70
71
            return $this;
72
        });
73
    }
74
75
    /**
76
     * @override
77
     * @inheritDoc
78
     */
79 View Code Duplication
    public function commit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
       return $this->connector->execute('COMMIT')->success(function (CommandResultStatement $result) {
82
           if ($result) {
83
               $this->emitter->emit('transaction:end');
0 ignored issues
show
Bug introduced by
The property emitter does not seem to exist. Did you mean emitterBlocked?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
84
           }
85
86
           return $result;
87
       });
88
    }
89
90
    /**
91
     * @override
92
     * @inheritDoc
93
     */
94 View Code Duplication
    public function rollback()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
       return $this->connector->execute('ROLLBACK')->success(function (CommandResultStatement $result) {
97
           if ($result) {
98
               $this->emitter->emit('transaction:end');    
0 ignored issues
show
Bug introduced by
The property emitter does not seem to exist. Did you mean emitterBlocked?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99
           }
100
101
           return $result;
102
       });
103
    }
104
}
105