Transaction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 81
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getError() 0 3 1
A getConnection() 0 3 1
A getResults() 0 3 1
A hasError() 0 3 1
A __construct() 0 18 4
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
            $connection = $this->getConnection();
40
            try {
41
                $connection->beginTransaction();
42
                $this->results = $closure($connection);
43
                if ($this->results === false) {
44
                    $connection->rollbackTransaction();
45
                } else {
46
                    $connection->commitTransaction();
47
                }
48
            } catch (\Exception $e) {
49
                $connection->rollbackTransaction();
50
                $this->error = $e;
51
            }
52
        }
53
        return $this;
54
    }
55
56
    /**
57
     * Gets current connection.
58
     * @param  string $name
59
     * @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...
60
     */
61
    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

61
    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...
62
    {
63
        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...
64
    }
65
66
    /**
67
     * Gets query results.
68
     * @return array
69
     */
70
    public function getResults()
71
    {
72
        return $this->results;
73
    }
74
75
    /**
76
     * Gets transacion's error.
77
     * @return mixed
78
     */
79
    public function getError()
80
    {
81
        return $this->error;
82
    }
83
84
    /**
85
     * Checks if transacion has error.
86
     * @return boolean
87
     */
88
    public function hasError()
89
    {
90
        return isset($this->error);
91
    }
92
}
93