Passed
Push — next ( c138b3...560f45 )
by Bas
12:33 queued 10:38
created

SupportsTransactions   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 129
ccs 25
cts 26
cp 0.9615
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A commit() 0 3 1
A abort() 0 3 1
A transactions() 0 7 2
A setTransactionManager() 0 3 1
A transactionAwareRequest() 0 17 3
A getTransactionManager() 0 3 1
A rollBack() 0 3 1
A begin() 0 3 1
A beginTransaction() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArangoClient\Transactions;
6
7
use ArangoClient\Exceptions\ArangoException;
8
use ArangoClient\Http\HttpRequestOptions;
9
use stdClass;
10
11
trait SupportsTransactions
12
{
13
    /**
14
     * @var TransactionManager|null
15
     */
16
    protected ?TransactionManager $transactionManager = null;
17
18
    /**
19
     * @return TransactionManager
20
     */
21 18
    public function transactions(): TransactionManager
22
    {
23 18
        if (! isset($this->transactionManager)) {
24 18
            $this->transactionManager = new TransactionManager($this);
25
        }
26
27 18
        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...
28
    }
29
30
    /**
31
     * Shortcut to begin() on the transactionManager
32
     *
33
     * @param  array<string, array<string>>  $collections
34
     * @param  array<mixed>  $options
35
     * @return string
36
     *
37
     * @throws ArangoException
38
     */
39 1
    public function begin(array $collections = [], array $options = []): string
40
    {
41 1
        return $this->transactions()->begin($collections, $options);
42
    }
43
44
    /**
45
     * Shortcut to begin() on the transactionManager
46
     *
47
     * @param  array<string, array<string>>  $collections
48
     * @param  array<mixed>  $options
49
     * @return string
50
     *
51
     * @throws ArangoException
52
     */
53 4
    public function beginTransaction(array $collections = [], array $options = []): string
54
    {
55 4
        return $this->transactions()->begin($collections, $options);
56
    }
57
58
    /**
59
     * Shortcut to abort() on the transactionManager
60
     *
61
     * @param  string|null  $id
62
     * @return bool
63
     *
64
     * @throws ArangoException
65
     */
66 3
    public function abort(string $id = null): bool
67
    {
68 3
        return $this->transactions()->abort($id);
69
    }
70
71
    /**
72
     * Shortcut to abort() on the transactionManager
73
     *
74
     * @param  string|null  $id
75
     * @return bool
76
     *
77
     * @throws ArangoException
78
     */
79 1
    public function rollBack(string $id = null): bool
80
    {
81 1
        return $this->transactions()->abort($id);
82
    }
83
84
    /**
85
     * Shortcut to commit() on the transactionManager
86
     *
87
     * @param  string|null  $id
88
     * @return bool
89
     *
90
     * @throws ArangoException
91
     */
92 1
    public function commit(string $id = null): bool
93
    {
94 1
        return $this->transactions()->commit($id);
95
    }
96
97
    /**
98
     * @psalm-suppress MixedReturnStatement
99
     *
100
     * @param  string  $method
101
     * @param  string  $uri
102
     * @param  array<mixed>|HttpRequestOptions  $options
103
     * @param  string|null  $database
104
     * @param  int|null  $transactionId
105
     * @return stdClass
106
     *
107
     * @throws ArangoException
108
     */
109 13
    public function transactionAwareRequest(
110
        string $method,
111
        string $uri,
112
        $options = [],
113
        ?string $database = null,
114
        ?int $transactionId = null
115
    ): stdClass {
116 13
        if (is_array($options)) {
117 13
            $options = $this->prepareRequestOptions($options);
0 ignored issues
show
Bug introduced by
It seems like prepareRequestOptions() 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

117
            /** @scrutinizer ignore-call */ 
118
            $options = $this->prepareRequestOptions($options);
Loading history...
118
        }
119
        try {
120 13
            if (! isset($transactionId)) {
121 13
                $transactionId = $this->transactions()->getTransaction();
122
            }
123 1
            $options->addHeader('x-arango-trx-id', $transactionId);
124
        } finally {
125 13
            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

125
            return $this->/** @scrutinizer ignore-call */ request($method, $uri, $options, $database);
Loading history...
126
        }
127
    }
128
129 1
    public function setTransactionManager(TransactionManager $transactionManager): void
130
    {
131 1
        $this->transactionManager = $transactionManager;
132
    }
133
134
    /**
135
     * @return TransactionManager|null
136
     */
137 1
    public function getTransactionManager(): ?TransactionManager
138
    {
139 1
        return $this->transactionManager;
140
    }
141
}
142