TransactionMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Tactician\Doctrine\DBAL;
6
7
use Doctrine\DBAL\Driver\Connection;
8
use Exception;
9
use League\Tactician\Middleware;
10
use Throwable;
11
12
/**
13
 * Wraps command execution inside a Doctrine DBAL transaction
14
 */
15
class TransactionMiddleware implements Middleware
16
{
17
    protected Connection $connection;
18
19
    public function __construct(Connection $connection)
20
    {
21
        $this->connection = $connection;
22
    }
23
24
    /**
25
     * Executes the given command and optionally returns a value
26
     *
27
     * @return mixed
28
     *
29
     * @throws Exception
30
     * @throws Throwable
31
     */
32
    public function execute(object $command, callable $next)
33
    {
34
        $this->connection->beginTransaction();
35
36
        try {
37
            $returnValue = $next($command);
38
39
            $this->connection->commit();
40
        } catch (Exception $exception) {
41
            $this->connection->rollBack();
42
43
            throw $exception;
44
        } catch (Throwable $exception) {
45
            $this->connection->rollBack();
46
47
            throw $exception;
48
        }
49
50
        return $returnValue;
51
    }
52
}
53