Completed
Pull Request — master (#14)
by
unknown
03:56
created

TransactionMiddleware::execute()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 5
nop 2
1
<?php
2
3
namespace League\Tactician\Doctrine\DBAL;
4
5
use Doctrine\DBAL\Driver\Connection;
6
use League\Tactician\Middleware;
7
use Exception;
8
use Throwable;
9
10
/**
11
 * Wraps command execution inside a Doctrine DBAL transaction
12
 */
13
class TransactionMiddleware implements Middleware
14
{
15
    /**
16
     * @var Connection
17
     */
18
    protected $connection;
19
20
    /**
21
     * @param Connection $connection
22
     */
23
    public function __construct(Connection $connection)
24
    {
25
        $this->connection = $connection;
26
    }
27
28
    /**
29
     * Executes the given command and optionally returns a value
30
     *
31
     * @param object $command
32
     * @param callable $next
33
     * @return mixed
34
     * @throws Exception
35
     * @throws Throwable
36
     */
37 View Code Duplication
    public function execute($command, callable $next)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $this->connection->beginTransaction();
40
41
        try {
42
            $returnValue = $next($command);
43
44
            $this->connection->commit();
45
        } catch (Exception $exception) {
46
            $this->connection->rollBack();
47
48
            throw $exception;
49
        } 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...
50
            $this->connection->rollBack();
51
52
            throw $exception;
53
        }
54
55
        return $returnValue;
56
    }
57
}
58