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

TransactionMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 44.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 20
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 20 20 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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