Passed
Push — remanufacture/json-object-deco... ( 07d964...ba9553 )
by Bas
03:04 queued 01:15
created

SupportsTransactions   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 121
ccs 26
cts 28
cp 0.9286
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setTransactionManager() 0 3 1
A commit() 0 3 1
A abort() 0 3 1
A transactions() 0 6 2
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 17
    public function transactions(): TransactionManager
22
    {
23 17
        if (! isset($this->transactionManager)) {
24 17
            $this->transactionManager = new TransactionManager($this);
25
        }
26 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...
27
    }
28
29
    /**
30
     * Shortcut to begin() on the transactionManager
31
     *
32
     * @param  array<string, array<string>>  $collections
33
     * @param  array<mixed>  $options
34
     * @return string
35
     * @throws ArangoException
36
     */
37 1
    public function begin(array $collections = [], array $options = []): string
38
    {
39 1
        return $this->transactions()->begin($collections, $options);
40
    }
41
42
    /**
43
     * Shortcut to begin() on the transactionManager
44
     *
45
     * @param  array<string, array<string>>  $collections
46
     * @param  array<mixed>  $options
47
     * @return string
48
     * @throws ArangoException
49
     */
50 4
    public function beginTransaction(array $collections = [], array $options = []): string
51
    {
52 4
        return $this->transactions()->begin($collections, $options);
53
    }
54
55
    /**
56
     * Shortcut to abort() on the transactionManager
57
     *
58
     * @param  string|null  $id
59
     * @return bool
60
     * @throws ArangoException
61
     */
62 3
    public function abort(string $id = null): bool
63
    {
64 3
        return $this->transactions()->abort($id);
65
    }
66
67
    /**
68
     * Shortcut to abort() on the transactionManager
69
     *
70
     * @param  string|null  $id
71
     * @return bool
72
     * @throws ArangoException
73
     */
74 1
    public function rollBack(string $id = null): bool
75
    {
76 1
        return $this->transactions()->abort($id);
77
    }
78
79
    /**
80
     * Shortcut to commit() on the transactionManager
81
     * @param  string|null  $id
82
     * @return bool
83
     * @throws ArangoException
84
     */
85 1
    public function commit(string $id = null): bool
86
    {
87 1
        return $this->transactions()->commit($id);
88
    }
89
90
    /**
91
     * @psalm-suppress MixedReturnStatement
92
     *
93
     * @param  string  $method
94
     * @param  string  $uri
95
     * @param  array<mixed>|HttpRequestOptions  $options
96
     * @param  string|null  $database
97
     * @param  int|null  $transactionId
98
     * @return stdClass
99
     * @throws ArangoException
100
     */
101 12
    public function transactionAwareRequest(
102
        string $method,
103
        string $uri,
104
        $options = [],
105
        ?string $database = null,
106
        ?int $transactionId = null
107
    ): stdClass {
108 12
        if (is_array($options)) {
109 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

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

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