Passed
Push — main ( b7653b...a2f1c7 )
by Michael
09:49 queued 06:09
created

HasDatabaseTransactions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
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 61
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
     * @var bool
15
     */
16
    protected bool $useTransaction = false;
17
18
    /**
19
     * Enable transaction in pipeline.
20
     *
21
     * @return static
22
     */
23 3
    public function withTransaction(): static
24
    {
25 3
        $this->useTransaction = true;
26
27 3
        return $this;
28
    }
29
30
    /**
31
     * Begin the transaction if enabled.
32
     *
33
     * @return void
34
     */
35 25
    protected function beginTransaction(): void
36
    {
37 25
        if (! $this->useTransaction) {
38 22
            return;
39
        }
40
41 3
        DB::beginTransaction();
42
    }
43
44
    /**
45
     * Commit the transaction if enabled.
46
     *
47
     * @return void
48
     */
49 13
    protected function commitTransaction(): void
50
    {
51 13
        if (! $this->useTransaction) {
52 12
            return;
53
        }
54
55 1
        DB::commit();
56
    }
57
58
    /**
59
     * Rollback the transaction if enabled.
60
     *
61
     * @return void
62
     */
63 8
    protected function rollbackTransaction(): void
64
    {
65 8
        if (! $this->useTransaction) {
66 6
            return;
67
        }
68
69 2
        DB::rollBack();
70
    }
71
}
72