TransactionalInterceptor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 39
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 0 23 4
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