TransactionalInterceptor::invoke()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 14
cp 0.9286
rs 9.552
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4.0058
1
<?php
2
/**
3
 * This file is part of the Ray.AuraSqlModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\AuraSqlModule;
8
9
use Aura\Sql\ExtendedPdoInterface;
10
use Ray\Aop\MethodInterceptor;
11
use Ray\Aop\MethodInvocation;
12
use Ray\Aop\ReflectionMethod;
13
use Ray\AuraSqlModule\Annotation\Transactional;
14
use Ray\AuraSqlModule\Exception\RollbackException;
15
16
class TransactionalInterceptor implements MethodInterceptor
17
{
18
    /**
19
     * @var ExtendedPdoInterface | null
20
     */
21
    private $pdo;
22
23 4
    public function __construct(ExtendedPdoInterface $pdo = null)
24
    {
25 4
        $this->pdo = $pdo;
26 4
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 3
    public function invoke(MethodInvocation $invocation)
32
    {
33
        /** @var ReflectionMethod $method */
34 3
        $method = $invocation->getMethod();
35
        /** @var Transactional $transactional */
36 3
        $transactional = $method->getAnnotation(Transactional::class);
37 3
        if (\count($transactional->value) > 1) {
0 ignored issues
show
Deprecated Code introduced by
The property Ray\AuraSqlModule\Annotation\Transactional::$value has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
38 1
            return (new PropTransaction)($invocation, $transactional);
39
        }
40 2
        if (! $this->pdo instanceof ExtendedPdoInterface) {
41
            return $invocation->proceed();
42
        }
43
        try {
44 2
            $this->pdo->beginTransaction();
45 2
            $result = $invocation->proceed();
46 1
            $this->pdo->commit();
47 1
        } catch (\PDOException $e) {
48 1
            $this->pdo->rollBack();
49 1
            throw new RollbackException($e->getMessage(), 0, $e);
50
        }
51
52 1
        return $result;
53
    }
54
}
55