Failed Conditions
Push — next ( 1e4ade...f47ec0 )
by Bas
04:16 queued 11s
created

SupportsTransactions   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 124
ccs 22
cts 30
cp 0.7332
rs 10
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A commit() 0 3 1
A abort() 0 3 1
A transactions() 0 6 2
A transactionAwareRequest() 0 20 4
A rollBack() 0 3 1
A beginTransaction() 0 3 1
A setTransactionManager() 0 3 1
A getTransactionManager() 0 3 1
A begin() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArangoClient\Transactions;
6
7
use ArangoClient\Exceptions\ArangoException;
8
9
trait SupportsTransactions
10
{
11
    /**
12
     * @var TransactionManager|null
13
     */
14
    protected ?TransactionManager $transactionManager = null;
15
16
    /**
17
     * @return TransactionManager
18
     */
19 17
    public function transactions(): TransactionManager
20
    {
21 17
        if (! isset($this->transactionManager)) {
22 17
            $this->transactionManager = new TransactionManager($this);
23
        }
24 17
        return $this->transactionManager;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transactionManager could return the type null which is incompatible with the type-hinted return ArangoClient\Transactions\TransactionManager. Consider adding an additional type-check to rule them out.
Loading history...
25
    }
26
27
    /**
28
     * Shortcut to begin() on the transactionManager
29
     *
30
     * @param  array{read?: string[], write?: string[], exclusive?: string[]}  $collections
31
     * @param  array<mixed>  $options
32
     * @return string
33
     * @throws ArangoException
34
     */
35 1
    public function begin(array $collections = [], array $options = []): string
36
    {
37 1
        return $this->transactions()->begin($collections, $options);
38
    }
39
40
    /**
41
     * Shortcut to begin() on the transactionManager
42
     *
43
     * @param  array{read?: string[], write?: string[], exclusive?: string[]}  $collections
44
     * @param  array<mixed>  $options
45
     * @return string
46
     * @throws ArangoException
47
     */
48 4
    public function beginTransaction(array $collections = [], array $options = []): string
49
    {
50 4
        return $this->transactions()->begin($collections, $options);
51
    }
52
53
    /**
54
     * Shortcut to abort() on the transactionManager
55
     *
56
     * @param  string|null  $id
57
     * @return bool
58
     * @throws ArangoException
59
     */
60 3
    public function abort(string $id = null): bool
61
    {
62 3
        return $this->transactions()->abort($id);
63
    }
64
65
    /**
66
     * Shortcut to abort() on the transactionManager
67
     *
68
     * @param  string|null  $id
69
     * @return bool
70
     * @throws ArangoException
71
     */
72 1
    public function rollBack(string $id = null): bool
73
    {
74 1
        return $this->transactions()->abort($id);
75
    }
76
77
    /**
78
     * Shortcut to commit() on the transactionManager
79
     * @param  string|null  $id
80
     * @return bool
81
     * @throws ArangoException
82
     */
83 1
    public function commit(string $id = null): bool
84
    {
85 1
        return $this->transactions()->commit($id);
86
    }
87
88
    /**
89
     * @psalm-suppress MixedReturnStatement
90
     *
91
     * @param  string  $method
92
     * @param  string  $uri
93
     * @param  array<mixed>  $options
94
     * @param  string|null  $database
95
     * @param  int|null  $transactionId
96
     * @return array<mixed>
97
     * @throws ArangoException
98
     */
99 12
    public function transactionAwareRequest(
100
        string $method,
101
        string $uri,
102
        array $options = [],
103
        ?string $database = null,
104
        ?int $transactionId = null
105
    ): array {
106
        try {
107 12
            if (! isset($transactionId)) {
108 12
                $transactionId = $this->transactions()->getTransaction();
109
            }
110 1
            if (! isset($options['headers'])) {
111 1
                $options['headers'] = [];
112
            }
113 1
            if (! is_array($options['headers'])) {
114
                $options['headers'] = [$options['headers']];
115
            }
116 1
            $options['headers']['x-arango-trx-id'] = $transactionId;
117
        } finally {
118 12
            return $this->request($method, $uri, $options, $database);
0 ignored issues
show
Bug introduced by
It seems like request() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

118
            return $this->/** @scrutinizer ignore-call */ request($method, $uri, $options, $database);
Loading history...
119
        }
120
    }
121
122
    public function setTransactionManager(TransactionManager $transactionManager): void
123
    {
124
        $this->transactionManager = $transactionManager;
125
    }
126
127
    /**
128
     * @return TransactionManager|null
129
     */
130
    public function getTransactionManager(): ?TransactionManager
131
    {
132
        return $this->transactionManager;
133
    }
134
}
135