Completed
Push — 1.x ( 249f8e...9ce618 )
by Akihito
11s
created

TransactionalInterceptor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
eloc 15
c 7
b 2
f 0
dl 0
loc 33
ccs 15
cts 16
cp 0.9375
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A invoke() 0 24 5
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 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);
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 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