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

DeleteModule::handle()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0811

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
ccs 8
cts 11
cp 0.7272
rs 9.9
c 0
b 0
f 0
cc 2
nc 7
nop 1
crap 2.0811
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