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

DeleteModule::appendDeleteMarker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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