RollbackOnlyTransactionMiddleware::execute()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 20
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Tactician\Doctrine\ORM;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Exception;
9
use League\Tactician\Middleware;
10
use Throwable;
11
12
class RollbackOnlyTransactionMiddleware implements Middleware
13
{
14
    private EntityManagerInterface $entityManager;
15
16
    public function __construct(EntityManagerInterface $entityManager)
17
    {
18
        $this->entityManager = $entityManager;
19
    }
20
21
    /**
22
     * Executes the given command and optionally returns a value
23
     *
24
     * @return mixed
25
     *
26
     * @throws Throwable
27
     * @throws Exception
28
     */
29
    public function execute(object $command, callable $next)
30
    {
31
        $this->entityManager->beginTransaction();
32
33
        try {
34
            $returnValue = $next($command);
35
36
            $this->entityManager->flush();
37
            $this->entityManager->commit();
38
        } catch (Exception $e) {
39
            $this->entityManager->rollback();
40
41
            throw $e;
42
        } catch (Throwable $e) {
43
            $this->entityManager->rollback();
44
45
            throw $e;
46
        }
47
48
        return $returnValue;
49
    }
50
}
51