Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DoctrineTransactionProvider
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 transactional
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Shared\Infrastructure\Service;
4
5use Doctrine\ORM\EntityManagerInterface;
6use Override;
7use Throwable;
8
9final readonly class DoctrineTransactionProvider implements \App\Shared\Domain\Port\Driven\TransactionProvider
10{
11
12    public function __construct(private EntityManagerInterface $entityManager)
13    {
14    }
15
16    /**
17     * @inheritDoc
18     */
19    #[Override]
20    public function transactional(callable $callback): mixed
21    {
22        $this->entityManager->beginTransaction();
23
24        try {
25            $result = $callback();
26            $this->entityManager->flush();
27            $this->entityManager->commit();
28            return $result;
29        } catch (Throwable $e) {
30            $this->entityManager->rollback();
31            // TODO log the transaction error
32            // then rethrow the exception as is, because it may (should ?) be a domain Exception with meaning
33            // throw new TransactionProviderException($e);
34            throw $e;
35        }
36    }
37}