Completed
Push — master ( 949287...c554df )
by Ross
9s
created

RollbackOnlyTransactionMiddleware::execute()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 21
Code Lines 13

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 7
nop 2
1
<?php
2
3
namespace League\Tactician\Doctrine\ORM;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Exception;
7
use League\Tactician\Middleware;
8
use Throwable;
9
10 View Code Duplication
class RollbackOnlyTransactionMiddleware implements Middleware
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    /**
13
     * @var EntityManagerInterface
14
     */
15
    private $entityManager;
16
17
    /**
18
     * @param EntityManagerInterface $entityManager
19
     */
20
    public function __construct(EntityManagerInterface $entityManager)
21
    {
22
        $this->entityManager = $entityManager;
23
    }
24
25
    /**
26
     * Executes the given command and optionally returns a value
27
     *
28
     * @param object $command
29
     * @param callable $next
30
     * @return mixed
31
     * @throws \Throwable
32
     * @throws \Exception
33
     */
34
    public function execute($command, callable $next)
35
    {
36
        $this->entityManager->beginTransaction();
37
38
        try {
39
            $returnValue = $next($command);
40
41
            $this->entityManager->flush();
42
            $this->entityManager->commit();
43
        } catch (Exception $e) {
44
            $this->entityManager->rollback();
45
46
            throw $e;
47
        } catch (Throwable $e) {
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...
48
            $this->entityManager->rollback();
49
50
            throw $e;
51
        }
52
53
        return $returnValue;
54
    }
55
}
56