Passed
Push — master ( 537eca...d2535c )
by Sergei
02:59
created

TransactionalTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 41
c 1
b 0
f 0
dl 0
loc 78
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isTransactional() 0 3 1
A transactions() 0 6 1
A insert() 0 20 4
A delete() 0 16 3
A update() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Trait;
6
7
use Throwable;
8
use Yiisoft\ActiveRecord\ActiveRecordInterface;
9
use Yiisoft\ActiveRecord\TransactionalInterface;
10
11
use function in_array;
12
13
/**
14
 * Trait to implement transactional operations and {@see TransactionalInterface} for ActiveRecord.
15
 *
16
 * @see ActiveRecordInterface::insert()
17
 * @see ActiveRecordInterface::update()
18
 * @see ActiveRecordInterface::delete()
19
 */
20
trait TransactionalTrait
21
{
22
    public function delete(): int
23
    {
24
        if (!$this->isTransactional(TransactionalInterface::OP_DELETE)) {
25
            return $this->deleteInternal();
0 ignored issues
show
Bug introduced by
The method deleteInternal() does not exist on Yiisoft\ActiveRecord\Trait\TransactionalTrait. Did you maybe mean delete()? ( Ignorable by Annotation )

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

25
            return $this->/** @scrutinizer ignore-call */ deleteInternal();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
        }
27
28
        $transaction = $this->db->beginTransaction();
29
30
        try {
31
            $result = $this->deleteInternal();
32
            $transaction->commit();
33
34
            return $result;
35
        } catch (Throwable $e) {
36
            $transaction->rollBack();
37
            throw $e;
38
        }
39
    }
40
41
    public function insert(array $attributes = null): bool
42
    {
43
        if (!$this->isTransactional(TransactionalInterface::OP_INSERT)) {
44
            return $this->insertInternal($attributes);
0 ignored issues
show
Bug introduced by
The method insertInternal() does not exist on Yiisoft\ActiveRecord\Trait\TransactionalTrait. Did you maybe mean insert()? ( Ignorable by Annotation )

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

44
            return $this->/** @scrutinizer ignore-call */ insertInternal($attributes);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        }
46
47
        $transaction = $this->db->beginTransaction();
48
49
        try {
50
            $result = $this->insertInternal($attributes);
51
            if ($result === false) {
52
                $transaction->rollBack();
53
            } else {
54
                $transaction->commit();
55
            }
56
57
            return $result;
58
        } catch (Throwable $e) {
59
            $transaction->rollBack();
60
            throw $e;
61
        }
62
    }
63
64
    public function isTransactional(int $operation): bool
65
    {
66
        return in_array($operation, $this->transactions(), true);
67
    }
68
69
    public function transactions(): array
70
    {
71
        return [
72
            TransactionalInterface::OP_INSERT,
73
            TransactionalInterface::OP_UPDATE,
74
            TransactionalInterface::OP_DELETE,
75
        ];
76
    }
77
78
    public function update(array $attributeNames = null): int
79
    {
80
        if (!$this->isTransactional(TransactionalInterface::OP_UPDATE)) {
81
            return $this->updateInternal($attributeNames);
0 ignored issues
show
Bug introduced by
The method updateInternal() does not exist on Yiisoft\ActiveRecord\Trait\TransactionalTrait. Did you maybe mean update()? ( Ignorable by Annotation )

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

81
            return $this->/** @scrutinizer ignore-call */ updateInternal($attributeNames);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
        }
83
84
        $transaction = $this->db->beginTransaction();
85
86
        try {
87
            $result = $this->updateInternal($attributeNames);
88
            if ($result === 0) {
89
                $transaction->rollBack();
90
            } else {
91
                $transaction->commit();
92
            }
93
94
            return $result;
95
        } catch (Throwable $e) {
96
            $transaction->rollBack();
97
            throw $e;
98
        }
99
    }
100
}
101