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

TransactionalInterceptor::invoke()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 3
b 0
f 0
nc 6
nop 1
dl 0
loc 25
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.4222
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