Passed
Push — main ( f754fa...2a1ce4 )
by Gaetano
09:34
created

TransactionManagerTrait::resetDBTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core;
4
5
use Doctrine\DBAL\Connection;
6
use eZ\Publish\API\Repository\Repository;
7
use ProxyManager\Proxy\ValueHolderInterface;
8
9
/**
10
 * Functionality related to managing transactions - both "repo transactions" and "db transactions"
11
 *
12
 * Implemented as a trait to avoid breaking BC as much as possible.
13
 * @todo transform this into an interface + a service, when bumping a major version
14
 */
15
trait TransactionManagerTrait
16
{
17
    /** @var Repository $repository */
18
    protected $repository;
19
20
    /** @var Connection $connection */
21
    protected $connection;
22
23
    public function setRepository(Repository $repository): void
24
    {
25
        // NB: ideally we should retrieve the DB connection from the Repository. But that means going through
26
        // multiple steps of access to protected/private members (persistenceHandler / transactionHandler / ...) which
27
        // are not guaranteed to be stable, or even present. So we inject th connection separately...
28
29
        $this->repository = $repository;
30
    }
31
32
    public function setConnection(Connection $connection): void
33
    {
34
        $this->connection = $connection;
35
    }
36
37
    /**
38
     * Repo transaction
39
     * @return void
40
     */
41
    protected function beginTransaction(): void
42
    {
43
        $this->repository->beginTransaction();
44
    }
45
46
    /**
47
     * Repo transaction
48
     * @return void
49
     */
50
    protected function commit(): void
51
    {
52
        $this->repository->commit();
53
    }
54
55
    /**
56
     * Repo transaction
57
     * @return void
58
     */
59
    protected function rollback(): void
60
    {
61
        $this->repository->rollback();
62
    }
63
64
    /**
65
     * Resets the transaction counter and all other transaction-related info for the current db connection.
66
     * To be used only when we know for sure the db has no active transactions, and the DBAL object is out of sync
67
     * @internal
68
     * @return void
69
     */
70
    protected function resetDBTransaction()
71
    {
72
        /** @var Connection $connection */
73
        $connection = ($this->connection instanceof ValueHolderInterface) ? $this->connection->getWrappedValueHolderValue() : $this->connection;
74
        $cl = \Closure::bind(function () {
75
            $this->transactionNestingLevel = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property transactionNestingLevel does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
            $this->isRollbackOnly = false;
0 ignored issues
show
Bug Best Practice introduced by
The property isRollbackOnly does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
        },
78
            $connection,
79
            $connection
80
        );
81
        $cl();
82
    }
83
84
    /**
85
     * Returns the current db transaction nesting level.
86
     *
87
     * @return int The nesting level. A value of 0 means there's no active transaction.
88
     */
89
    public function getDBTransactionNestingLevel()
90
    {
91
        return $this->connection->getTransactionNestingLevel();
92
    }
93
94
    public function rollbackDBTransaction()
95
    {
96
        return $this->connection->rollBack();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->connection->rollBack() targeting Doctrine\DBAL\Connection::rollBack() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
97
    }
98
}
99