Passed
Push — next ( d904ac...c0af08 )
by Bas
13:43
created

SupportsTransactions::transactions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 0
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
10
trait SupportsTransactions
11
{
12
    /**
13
     * @var TransactionManager|null
14
     */
15
    protected ?TransactionManager $transactionManager = null;
16
17
    /**
18
     * @return TransactionManager
19
     */
20 17
    public function transactions(): TransactionManager
21
    {
22 17
        if (! isset($this->transactionManager)) {
23 17
            $this->transactionManager = new TransactionManager($this);
24
        }
25 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...
26
    }
27
28
    /**
29
     * Shortcut to begin() on the transactionManager
30
     *
31
     * @param  array{read?: string[], write?: string[], exclusive?: string[]}  $collections
32
     * @param  array<mixed>  $options
33
     * @return string
34
     * @throws ArangoException
35
     */
36 1
    public function begin(array $collections = [], array $options = []): string
37
    {
38 1
        return $this->transactions()->begin($collections, $options);
39
    }
40
41
    /**
42
     * Shortcut to begin() on the transactionManager
43
     *
44
     * @param  array{read?: string[], write?: string[], exclusive?: string[]}  $collections
45
     * @param  array<mixed>  $options
46
     * @return string
47
     * @throws ArangoException
48
     */
49 4
    public function beginTransaction(array $collections = [], array $options = []): string
50
    {
51 4
        return $this->transactions()->begin($collections, $options);
52
    }
53
54
    /**
55
     * Shortcut to abort() on the transactionManager
56
     *
57
     * @param  string|null  $id
58
     * @return bool
59
     * @throws ArangoException
60
     */
61 3
    public function abort(string $id = null): bool
62
    {
63 3
        return $this->transactions()->abort($id);
64
    }
65
66
    /**
67
     * Shortcut to abort() on the transactionManager
68
     *
69
     * @param  string|null  $id
70
     * @return bool
71
     * @throws ArangoException
72
     */
73 1
    public function rollBack(string $id = null): bool
74
    {
75 1
        return $this->transactions()->abort($id);
76
    }
77
78
    /**
79
     * Shortcut to commit() on the transactionManager
80
     * @param  string|null  $id
81
     * @return bool
82
     * @throws ArangoException
83
     */
84 1
    public function commit(string $id = null): bool
85
    {
86 1
        return $this->transactions()->commit($id);
87
    }
88
89
    /**
90
     * @psalm-suppress MixedReturnStatement
91
     *
92
     * @param  string  $method
93
     * @param  string  $uri
94
     * @param  array<mixed>|HttpRequestOptions  $options
95
     * @param  string|null  $database
96
     * @param  int|null  $transactionId
97
     * @return array<mixed>
98
     * @throws ArangoException
99
     */
100 12
    public function transactionAwareRequest(
101
        string $method,
102
        string $uri,
103
        $options = [],
104
        ?string $database = null,
105
        ?int $transactionId = null
106
    ): array {
107 12
        if (is_array($options)) {
108 12
            $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

108
            /** @scrutinizer ignore-call */ 
109
            $options = $this->prepareRequestOptions($options);
Loading history...
109
        }
110
        try {
111 12
            if (! isset($transactionId)) {
112 12
                $transactionId = $this->transactions()->getTransaction();
113
            }
114 1
            $options->addHeader('x-arango-trx-id', $transactionId);
115
        } finally {
116 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

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