DatabaseTransactions::beginDatabaseTransaction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 27
ccs 19
cts 19
cp 1
crap 3
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Testing;
6
7
use Illuminate\Foundation\Testing\DatabaseTransactions as IlluminateDatabaseTransactions;
8
use Illuminate\Foundation\Testing\DatabaseTransactionsManager;
9
use LaravelFreelancerNL\Aranguent\Testing\Concerns\PreparesTestingTransactions;
10
11
trait DatabaseTransactions
12
{
13
    use PreparesTestingTransactions;
14
    use IlluminateDatabaseTransactions;
15
16
    /**
17
     * Handle database transactions on the specified connections.
18
     *
19
     * @return void
20
     */
21 133
    public function beginDatabaseTransaction()
22
    {
23 133
        $database = $this->app->make('db');
24
25 133
        $connections = $this->connectionsToTransact();
26
27 133
        $this->app->instance('db.transactions', $transactionsManager = new DatabaseTransactionsManager($connections));
28
29 133
        foreach ($this->connectionsToTransact() as $name) {
30 133
            $connection = $database->connection($name);
31 133
            $connection->setTransactionManager($transactionsManager);
32 133
            $dispatcher = $connection->getEventDispatcher();
33
34 133
            $connection->unsetEventDispatcher();
35 133
            $connection->beginTransaction($this->transactionCollections);
36 133
            $connection->setEventDispatcher($dispatcher);
37
        }
38
39 133
        $this->beforeApplicationDestroyed(function () use ($database) {
0 ignored issues
show
Bug introduced by
It seems like beforeApplicationDestroyed() 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

39
        $this->/** @scrutinizer ignore-call */ 
40
               beforeApplicationDestroyed(function () use ($database) {
Loading history...
40 133
            foreach ($this->connectionsToTransact() as $name) {
41 133
                $connection = $database->connection($name);
42 133
                $dispatcher = $connection->getEventDispatcher();
43
44 133
                $connection->unsetEventDispatcher();
45 133
                $connection->rollBack();
46 133
                $connection->setEventDispatcher($dispatcher);
47 133
                $connection->disconnect();
48
            }
49 133
        });
50
    }
51
}
52