Passed
Push — next ( 09b52e...659675 )
by Bas
13:30 queued 11:38
created

SupportsTransactions::transactionAwareRequest()   A

Complexity

Conditions 3
Paths 10

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 10
nop 5
dl 0
loc 17
ccs 7
cts 7
cp 1
crap 3
rs 10
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 18
    public function transactions(): TransactionManager
19
    {
20 18
        if (! (property_exists($this, 'transactionManager') && $this->transactionManager !== null)) {
21 18
            $this->transactionManager = new TransactionManager($this);
22
        }
23
24 18
        return $this->transactionManager;
25
    }
26
27
    /**
28
     * Shortcut to begin() on the transactionManager
29
     *
30
     * @param  array<string, array<string>>  $collections
31
     * @param  array<mixed>  $options
32
     *
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<string, array<string>>  $collections
44
     * @param  array<mixed>  $options
45
     *
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
     *
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
     *
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
     *
80
     * @param  string|null  $id
81
     *
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
     * @param  array<mixed>|HttpRequestOptions  $options
91
     *
92
     * @throws ArangoException
93
     */
94 13
    public function transactionAwareRequest(
95
        string $method,
96
        string $uri,
97
        array|HttpRequestOptions $options = [],
98
        ?string $database = null,
99
        ?int $transactionId = null
100
    ): stdClass {
101 13
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
102 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

102
            /** @scrutinizer ignore-call */ 
103
            $options = $this->prepareRequestOptions($options);
Loading history...
103
        }
104
        try {
105 13
            if (! isset($transactionId)) {
106 13
                $transactionId = $this->transactions()->getTransaction();
107
            }
108 1
            $options->addHeader('x-arango-trx-id', $transactionId);
109
        } finally {
110 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

110
            return $this->/** @scrutinizer ignore-call */ request($method, $uri, $options, $database);
Loading history...
111
        }
112
    }
113
114 1
    public function setTransactionManager(TransactionManager $transactionManager): void
115
    {
116 1
        $this->transactionManager = $transactionManager;
117
    }
118
119 1
    public function getTransactionManager(): ?TransactionManager
120
    {
121 1
        return $this->transactionManager;
122
    }
123
}
124