Passed
Push — master ( ab171b...3e1bb9 )
by Philip
14:43
created

TransactionManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
    /**
17
     * @var LoggerInterface
18
     */
19
    private $logger;
20
21
    /**
22
     * @var EntityManagerInterface
23
     */
24
    private $entityManager;
25
26 22
    public function __construct(EntityManagerInterface $entityManager)
27
    {
28 22
        $this->entityManager = $entityManager;
29 22
        $this->logger = new NullLogger();
30 22
    }
31
32 22
    public function beginTransaction(): void
33
    {
34 22
        $this->entityManager->beginTransaction();
35 22
    }
36
37 20
    public function commitTransaction(): bool
38
    {
39 20
        $nestingLevel = $this->entityManager->getConnection()->getTransactionNestingLevel();
40 20
        $flush = false;
41
42
        /* No active transaction */
43 20
        if (!$this->isInTransaction()) {
44
            $this->logger->warning('No active Transaction for commit');
45
46
            return $flush;
47
        }
48
49
        /* Topmost transaction, flush */
50 20
        if (1 === $nestingLevel) {
51 20
            $flush = true;
52 20
            $this->entityManager->flush();
53
        }
54 20
        $this->entityManager->commit();
55
56 20
        return $flush;
57
    }
58
59 2
    public function rollbackTransaction(): void
60
    {
61
        /* No active transaction */
62 2
        if (!$this->isInTransaction()) {
63
            $this->logger->warning('No active Transaction for commit');
64
65
            return;
66
        }
67
68 2
        $this->entityManager->rollback();
69 2
    }
70
71 22
    public function isInTransaction(): bool
72
    {
73 22
        return 0 !== $this->entityManager->getConnection()->getTransactionNestingLevel();
74
    }
75
76 22
    public function transactional(Callable $func)
77
    {
78 22
        if (!is_callable($func)) {
79
            throw new InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"');
80
        }
81
82 22
        $this->beginTransaction();
83
84
        try {
85 22
            $return = call_user_func($func, $this);
86
87 20
            $this->commitTransaction();
88
89 20
            return $return;
90 2
        } catch (Exception $e) {
91 2
            $this->rollbackTransaction();
92
93 2
            throw $e;
94
        }
95
    }
96
97
    public function setLogger(LoggerInterface $logger): void
98
    {
99
        $this->logger = $logger;
100
    }
101
}
102