MorphCleanCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 15
dl 0
loc 27
rs 10
c 3
b 1
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A captureEvents() 0 11 2
A handle() 0 6 2
1
<?php
2
3
namespace Cesargb\Database\Support\Commands;
4
5
use Cesargb\Database\Support\Events\RelationMorphFromModelWasCleaned;
6
use Cesargb\Database\Support\Morph;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Event;
9
10
class MorphCleanCommand extends Command
11
{
12
    protected $signature = 'morph:clean
13
                                {--dry-run : test clean}';
14
15
    protected $description = 'Clean break relations morph';
16
17
    public function handle()
18
    {
19
        $this->captureEvents();
20
21
        if ((new Morph())->cleanResidualAllModels($this->option('dry-run')) === 0) {
0 ignored issues
show
Bug introduced by
$this->option('dry-run') of type string is incompatible with the type boolean expected by parameter $dryRun of Cesargb\Database\Support...leanResidualAllModels(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
        if ((new Morph())->cleanResidualAllModels(/** @scrutinizer ignore-type */ $this->option('dry-run')) === 0) {
Loading history...
22
            $this->info("\tIt's already cleaned");
23
        }
24
    }
25
26
    protected function captureEvents()
27
    {
28
        Event::listen(
29
            RelationMorphFromModelWasCleaned::class,
30
            function (RelationMorphFromModelWasCleaned $event) {
31
                $this->info(sprintf(
32
                    "\t✔ Clean model %s in the table %s: %d %s.",
33
                    get_class($event->model),
34
                    $event->relation->getRelated()->getTable(),
35
                    $event->numDeleted,
36
                    $event->dryRun ? 'rows to remove' : 'rows cleaned'
37
                ));
38
            }
39
        );
40
    }
41
}
42