TransactionMiddleware::execute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 19
rs 9.9
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