Passed
Pull Request — master (#324)
by Philippe
54:56 queued 21:29
created

DeleteModule::appendDeleteMarker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
            // Mark the slug as deleted to avoid any conflict with newly created modules with the same slug.
23 2
            $module->update([
24 2
                'slug' => $module->slug . $this->appendDeleteMarker(),
25
            ]);
26
27 2
            $module->delete();
28
29 2
            DB::commit();
30
        } catch (\Throwable $e) {
31
            DB::rollBack();
32
            throw $e;
33
        }
34 2
    }
35
36 2
    private function appendDeleteMarker(): string
37
    {
38 2
        return '_DELETED_' . time();
39
    }
40
}
41