HasDatabaseTransactions   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 13
c 0
b 0
f 0
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rollbackTransaction() 0 7 2
A beginTransaction() 0 7 2
A commitTransaction() 0 7 2
A withTransaction() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\EnhancedPipeline\Traits;
6
7
use Illuminate\Support\Facades\DB;
8
9
trait HasDatabaseTransactions
10
{
11
    /**
12
     * Determines whether class uses transaction.
13
     */
14
    protected bool $useTransaction = false;
15
16
    /**
17
     * Enable transaction in pipeline.
18
     */
19 3
    public function withTransaction(): static
20
    {
21 3
        $this->useTransaction = true;
22
23 3
        return $this;
24
    }
25
26
    /**
27
     * Begin the transaction if enabled.
28
     */
29 31
    protected function beginTransaction(): void
30
    {
31 31
        if (! $this->useTransaction) {
32 28
            return;
33
        }
34
35 3
        DB::beginTransaction();
36
    }
37
38
    /**
39
     * Commit the transaction if enabled.
40
     */
41 23
    protected function commitTransaction(): void
42
    {
43 23
        if (! $this->useTransaction) {
44 22
            return;
45
        }
46
47 1
        DB::commit();
48
    }
49
50
    /**
51
     * Rollback the transaction if enabled.
52
     */
53 8
    protected function rollbackTransaction(): void
54
    {
55 8
        if (! $this->useTransaction) {
56 6
            return;
57
        }
58
59 2
        DB::rollBack();
60
    }
61
}
62