FlushSequenceValues::handle()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 9.8333
1
<?php
2
3
namespace Gurgentil\LaravelEloquentSequencer\Console\Commands;
4
5
use Illuminate\Support\Facades\DB;
6
7
class FlushSequenceValues extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'sequence:flush {model : The model to be flushed}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Flush sequence values from a specific model.';
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return mixed
27
     */
28
    public function handle()
29
    {
30
        DB::transaction(function () {
31
            $class = $this->getModelArgument();
32
33
            $models = ($class)::all();
34
35
            if ($models->count() === 0) {
36
                $this->info('Nothing to update.');
37
38
                return;
39
            }
40
41
            $this->line("Analyzing and flushing sequence values from {$models->count()} object(s).");
42
43
            $columnName = ($class)::getSequenceColumnName();
44
45
            $modelsToUpdate = $models->where($columnName, '!=', null);
46
47
            $modelsToUpdate->each(function ($model) use ($columnName) {
48
                $model->withoutSequencing()->update([
49
                    $columnName => null,
50
                ]);
51
            });
52
53
            $this->info("{$modelsToUpdate->count()} row(s) were updated.");
54
        });
55
    }
56
}
57