Passed
Push — fix/radiobuttonstyling ( e57dd3...53f350 )
by Ben
15:46 queued 08:51
created

DeleteModule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 30
ccs 7
cts 10
cp 0.7
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 2
    public function handle($id)
14
    {
15
        try {
16 2
            DB::beginTransaction();
17
18 2
            $module = Module::findOrFail($id);
19
20 2
            Relation::deleteRelationsOf($module->getMorphClass(), $module->id);
21
22 2
            // Mark the slug as deleted to avoid any conflict with newly created modules with the same slug.
23
            $module->update([
24 2
                'slug' => $module->slug . $this->appendDeleteMarker(),
25
            ]);
26
27
            $module->delete();
28
29 2
            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