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
![]() |
|||
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 |