Passed
Push — fix/module-slug ( 15002a )
by Ben
09:40
created

DeleteModule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 2
A appendDeleteMarker() 0 3 1
1
<?php
2
namespace Thinktomorrow\Chief\Modules\Application;
3
4
use Illuminate\Support\Facades\DB;
5
use Thinktomorrow\Chief\Relations\Relation;
6
use Thinktomorrow\Chief\Modules\Module;
7
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableCommand;
8
9
class DeleteModule
10
{
11
    use TranslatableCommand;
12
13
    public function handle($id)
14
    {
15
        try {
16
            DB::beginTransaction();
17
18
            $module = Module::findOrFail($id);
19
20
            Relation::deleteRelationsOf($module->getMorphClass(), $module->id);
21
22
            // Mark the slug as deleted to avoid any conflict with newly created modules with the same slug.
23
            $module->update([
24
                'slug' => $module->slug . $this->appendDeleteMarker(),
25
            ]);
26
27
            $module->delete();
28
29
            DB::commit();
30
        } catch (\Throwable $e) {
31
            DB::rollBack();
32
            throw $e;
33
        }
34
    }
35
36
    private function appendDeleteMarker(): string
37
    {
38
        return '_DELETED_' . time();
39
    }
40
}
41