Passed
Push — main ( d65f74...8a9455 )
by Michael
03:18
created

HandlesDatabaseTransactions::rollbackTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
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
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\EnhancedPipeline\Traits;
6
7
use Illuminate\Support\Facades\DB;
8
9
trait HandlesDatabaseTransactions
10
{
11
    /**
12
     * Determines whether class uses transaction.
13
     *
14
     * @var bool
15
     */
16
    protected bool $useTransaction = false;
17
18
    /**
19
     * Begin the transaction if enabled.
20
     *
21
     * @return void
22
     */
23 20
    protected function beginTransaction(): void
24
    {
25 20
        if (! $this->useTransaction) {
26 18
            return;
27
        }
28
29 2
        DB::beginTransaction();
30
    }
31
32
    /**
33
     * Commit the transaction if enabled.
34
     *
35
     * @return void
36
     */
37 12
    protected function commitTransaction(): void
38
    {
39 12
        if (! $this->useTransaction) {
40 11
            return;
41
        }
42
43 1
        DB::commit();
44
    }
45
46
    /**
47
     * Rollback the transaction if enabled.
48
     *
49
     * @return void
50
     */
51 4
    protected function rollbackTransaction(): void
52
    {
53 4
        if (! $this->useTransaction) {
54 3
            return;
55
        }
56
57 1
        DB::rollBack();
58
    }
59
}
60