Passed
Push — 1.x ( 93745e...3395ce )
by Akihito
01:56 queued 45s
created

TransactionalInterceptor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 18
c 6
b 2
f 0
dl 0
loc 37
ccs 16
cts 17
cp 0.9412
rs 10
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ExtendedPdoInterface;
8
use Override;
9
use PDOException;
10
use Ray\Aop\MethodInterceptor;
11
use Ray\Aop\MethodInvocation;
12
use Ray\AuraSqlModule\Annotation\Transactional;
13
use Ray\AuraSqlModule\Exception\RollbackException;
14
15
use function count;
16
17
final readonly class TransactionalInterceptor implements MethodInterceptor
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 17 at column 6
Loading history...
18
{
19
    public function __construct(private ?ExtendedPdoInterface $pdo = null)
20
    {
21
    }
22
23 4
    /**
24
     * {@inheritDoc}
25 4
     */
26 4
    #[Override]
27
    public function invoke(MethodInvocation $invocation)
28
    {
29
        $method = $invocation->getMethod();
30
        $transactional = $method->getAnnotation(Transactional::class);
31 3
        if ($transactional instanceof Transactional && count((array) $transactional->value) > 1) {
32
            return (new PropTransaction())($invocation, $transactional);
33
        }
34 3
35
        if (! $this->pdo instanceof ExtendedPdoInterface) {
36 3
            return $invocation->proceed();
37 3
        }
38 1
39
        try {
40 2
            $this->pdo->beginTransaction();
41
            $result = $invocation->proceed();
42
            $this->pdo->commit();
43
        } catch (PDOException $e) {
44 2
            $this->pdo->rollBack();
45 2
46 1
            throw new RollbackException($e->getMessage(), 0, $e);
47 1
        }
48 1
49 1
        return $result;
50
    }
51
}
52