HasDatabaseTransactions::withTransaction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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