Passed
Push — master ( 32651b...bc7554 )
by Maike
02:30
created

Transaction::hasError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace MocOrm\Support;
4
5
use MocOrm\Connection\ConnectionManager;
6
7
/**
8
 * Implementation.
9
 */
10
class Transaction
11
{
12
    /**
13
     * Current transaction's database.
14
     * @var string
15
     */
16
    protected $db;
17
18
    /**
19
     * Transaction result.
20
     * @var array
21
     */
22
    protected $results;
23
24
    /**
25
     * SQL Query error.
26
     * @var mixed
27
     */
28
    protected $error;
29
30
31
    /**
32
     * Transacion
33
     * Responsible to holds transaction behave.
34
     * @param mixed $closure
35
     */
36
    public function __construct($closure = null)
37
    {
38
        if (!is_callable($closure)) {
39
            throw new \Exception("Transaction must have a callable as parameter.");
40
41
        } else {
42
            $connection = $this->getConnection();
43
44
            try {
45
                $connection->beginTransaction();
46
47
                $this->results = $closure($connection);
48
49
                if ($this->results === false) {
50
                    $connection->rollbackTransaction();
51
                } else {
52
                    $connection->commitTransaction();
53
                }
54
55
            } catch (\Exception $e) {
56
                $connection->rollbackTransaction();
57
                $this->error = $e;
58
            }
59
60
        }
61
        return $this;
62
    }
63
64
    /**
65
     * Gets current connection.
66
     * @param  string $name
67
     * @return \Connection
0 ignored issues
show
Bug introduced by
The type Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
     */
69
    public function getConnection($name = null)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

69
    public function getConnection(/** @scrutinizer ignore-unused */ $name = null)

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

Loading history...
70
    {
71
        return ConnectionManager::initialize()->current();
0 ignored issues
show
Bug Best Practice introduced by
The expression return MocOrm\Connection...initialize()->current() returns the type MocOrm\Connection\Connection which is incompatible with the documented return type Connection.
Loading history...
72
    }
73
74
    /**
75
     * Gets query results.
76
     * @return array
77
     */
78
    public function getResults()
79
    {
80
        return $this->results;
81
    }
82
83
    /**
84
     * Gets transacion's error.
85
     * @return mixed
86
     */
87
    public function getError()
88
    {
89
        return $this->error;
90
    }
91
92
    /**
93
     * Checks if transacion has error.
94
     * @return boolean
95
     */
96
    public function hasError()
97
    {
98
        return isset($this->error);
99
    }
100
}
101