Passed
Push — support-php-8.2-8.3 ( ae3b12...8c2247 )
by Akihito
01:21
created

TransactionalInterceptor::invoke()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 4
b 0
f 0
nc 6
nop 1
dl 0
loc 24
rs 9.4555
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 class TransactionalInterceptor implements MethodInterceptor
18
{
19
    public function __construct(private ?ExtendedPdoInterface $pdo = null)
20
    {
21
    }
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    #[Override]
27
    public function invoke(MethodInvocation $invocation)
28
    {
29
        $method = $invocation->getMethod();
30
        $transactional = $method->getAnnotation(Transactional::class);
31
        if ($transactional instanceof Transactional && count((array) $transactional->value) > 1) {
32
            return (new PropTransaction())($invocation, $transactional);
0 ignored issues
show
Deprecated Code introduced by
The class Ray\AuraSqlModule\PropTransaction has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

32
            return (/** @scrutinizer ignore-deprecated */ new PropTransaction())($invocation, $transactional);
Loading history...
33
        }
34
35
        if (! $this->pdo instanceof ExtendedPdoInterface) {
36
            return $invocation->proceed();
37
        }
38
39
        try {
40
            $this->pdo->beginTransaction();
41
            $result = $invocation->proceed();
42
            $this->pdo->commit();
43
        } catch (PDOException $e) {
44
            $this->pdo->rollBack();
45
46
            throw new RollbackException($e->getMessage(), 0, $e);
47
        }
48
49
        return $result;
50
    }
51
}
52