RollbackOnlyTransactionMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 20 3
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