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

DbHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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