TransactionMiddleware::execute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 12
nc 5
nop 2
1
<?php
2
3
namespace TillKruss\LaravelTactician\Middleware;
4
5
use Exception;
6
use Throwable;
7
use League\Tactician\Middleware;
8
use Illuminate\Database\DatabaseManager;
9
10
class TransactionMiddleware implements Middleware
11
{
12
    /**
13
     * The database manager instance.
14
     *
15
     * @var \Illuminate\Database\DatabaseManager
16
     */
17
    protected $database;
18
19
    /**
20
     * Create a new transaction middleware.
21
     *
22
     * @param \Illuminate\Database\DatabaseManager  $database
23
     */
24
    public function __construct(DatabaseManager $database)
25
    {
26
        $this->database = $database;
27
    }
28
29
    /**
30
     * Wrap a command execution in a database transaction.
31
     *
32
     * @param  object    $command
33
     * @param  callable  $next
34
     * @return mixed
35
     *
36
     * @throws Exception
37
     * @throws Throwable
38
     */
39
    public function execute($command, callable $next)
40
    {
41
        $this->database->beginTransaction();
42
43
        try {
44
            $returnValue = $next($command);
45
46
            $this->database->commit();
47
        } catch (Exception $exception) {
48
            $this->database->rollBack();
49
50
            throw $exception;
51
        } catch (Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
52
            $this->database->rollBack();
53
54
            throw $exception;
55
        }
56
57
        return $returnValue;
58
    }
59
}
60