Failed Conditions
Push — master ( 130b7c...9f9094 )
by Dallas
19:30 queued 14:09
created

DbHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 44
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateBatch() 0 6 1
A restoreBatchNumbers() 0 7 1
A __construct() 0 3 1
A backupBatchNumbers() 0 5 1
A checkIfRollbackIsSuccessful() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\RollbackMissingMigrations\Helpers;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Facades\DB;
10
use Umbrellio\RollbackMissingMigrations\Exceptions\RollbackMissingMigrationException;
11
12
class DbHelper
13
{
14
    private $migrationTable;
15
16 3
    public function __construct()
17
    {
18 3
        $this->migrationTable = Config::get('database.migrations');
19
    }
20
21 2
    public function backupBatchNumbers(array $migrationsNames): Collection
22
    {
23 2
        return DB::table($this->migrationTable)
24 2
            ->whereIn('migration', $migrationsNames)
25 2
            ->get();
26
    }
27
28 2
    public function updateBatch(array $migrationsNames, int $batchNumber)
29
    {
30 2
        DB::table($this->migrationTable)
31 2
            ->whereIn('migration', $migrationsNames)
32 2
            ->update([
33 2
                'batch' => $batchNumber,
34 2
            ]);
35
    }
36
37 2
    public function restoreBatchNumbers(Collection $backupData)
38
    {
39 2
        $backupData->each(function ($migrationData) {
40 2
            DB::table($this->migrationTable)
41 2
                ->where('migration', $migrationData->migration)
42 2
                ->update([
43 2
                    'batch' => $migrationData->batch,
44 2
                ]);
45 2
        });
46
    }
47
48 2
    public function checkIfRollbackIsSuccessful($migrationsForRollback): void
49
    {
50 2
        $migrations = DB::table($this->migrationTable)
51 2
            ->whereIn('migration', $migrationsForRollback)
52 2
            ->pluck('migration');
53
54 2
        if ($migrations->isNotEmpty()) {
55 1
            throw new RollbackMissingMigrationException($migrations);
56
        }
57
    }
58
}
59