OrmTransactionMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A deliver() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\MessageBus\Bus\DeliveryChain\Middleware;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Exception;
9
use Werkspot\MessageBus\Bus\DeliveryChain\MiddlewareInterface;
10
use Werkspot\MessageBus\Message\MessageInterface;
11
12
final class OrmTransactionMiddleware implements MiddlewareInterface
13
{
14
    /**
15
     * @var EntityManagerInterface
16
     */
17
    private $entityManager;
18
19 3
    public function __construct(EntityManagerInterface $entityManager)
20
    {
21 3
        $this->entityManager = $entityManager;
22 3
    }
23
24 3
    public function deliver(MessageInterface $message, callable $next): void
25
    {
26 3
        if ($this->entityManager->getConnection()->isTransactionActive()) {
27 2
            $next($message);
28
29 1
            return;
30
        }
31
32
        try {
33 3
            $this->entityManager->beginTransaction();
34 3
            $next($message);
35 2
            $this->entityManager->flush();
36 2
            $this->entityManager->commit();
37 1
        } catch (Exception $exception) {
38 1
            $this->entityManager->rollback();
39 1
            throw $exception;
40
        }
41 2
    }
42
}
43