OrmTransactionMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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