Completed
Push — master ( 4ab9f9...3724f6 )
by Philip
02:39
created

TransactionManager::rollbackTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0932
1
<?php
2
3
namespace Dontdrinkandroot\Repository;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Exception;
7
use InvalidArgumentException;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
11
/**
12
 * @author Philip Washington Sorst <[email protected]>
13
 */
14
class TransactionManager
15
{
16
    /** @var LoggerInterface */
17
    private $logger;
18
19
    /** @var EntityManagerInterface */
20
    private $entityManager;
21
22 22
    public function __construct(EntityManagerInterface $entityManager)
23
    {
24 22
        $this->entityManager = $entityManager;
25 22
        $this->logger = new NullLogger();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Psr\Log\NullLogger() of type object<Psr\Log\NullLogger> is incompatible with the declared type object<Psr\Log\LoggerInterface> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26 22
    }
27
28 22
    public function beginTransaction(): void
29
    {
30 22
        $this->entityManager->beginTransaction();
31 22
    }
32
33 20
    public function commitTransaction(bool $forceFlush = false): bool
34
    {
35 20
        $nestingLevel = $this->entityManager->getConnection()->getTransactionNestingLevel();
36
37
        /* No active transaction */
38 20
        if (!$this->isInTransaction()) {
39
            $this->logger->warning('No active Transaction for commit');
40
41
            return false;
42
        }
43
44 20
        $flushed = false;
45
        /* Topmost transaction, flush */
46 20
        if (1 === $nestingLevel || $forceFlush) {
47 20
            $flushed = true;
48 20
            $this->entityManager->flush();
49
        }
50 20
        $this->entityManager->commit();
51
52 20
        return $flushed;
53
    }
54
55 2
    public function rollbackTransaction(): void
56
    {
57
        /* No active transaction */
58 2
        if (!$this->isInTransaction()) {
59
            $this->logger->warning('No active Transaction for commit');
60
61
            return;
62
        }
63
64 2
        $this->entityManager->close();
65 2
        $this->entityManager->rollback();
66 2
    }
67
68 22
    public function isInTransaction(): bool
69
    {
70 22
        return 0 !== $this->entityManager->getConnection()->getTransactionNestingLevel();
71
    }
72
73 22
    public function transactional(Callable $func, bool $forceFlush = false)
74
    {
75 22
        if (!is_callable($func)) {
76
            throw new InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"');
77
        }
78
79 22
        $this->beginTransaction();
80
81
        try {
82 22
            $return = call_user_func($func, $this);
83
84 20
            $this->commitTransaction($forceFlush);
85
86 20
            return $return;
87 2
        } catch (Exception $e) {
88 2
            $this->rollbackTransaction();
89
90 2
            throw $e;
91
        }
92
    }
93
94
    public function setLogger(LoggerInterface $logger): void
95
    {
96
        $this->logger = $logger;
97
    }
98
}
99