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

DeletePage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 4
eloc 16
dl 0
loc 30
ccs 12
cts 15
cp 0.8
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 28 4
1
<?php
2
namespace Thinktomorrow\Chief\Pages\Application;
3
4
use Illuminate\Support\Facades\DB;
5
use Thinktomorrow\Chief\Modules\Module;
6
use Thinktomorrow\Chief\Pages\Page;
7
use Thinktomorrow\Chief\Audit\Audit;
8
use Thinktomorrow\Chief\Urls\UrlRecord;
9
10
class DeletePage
11
{
12 5
    public function handle($id)
13
    {
14
        try {
15 5
            DB::beginTransaction();
16
17 5
            $page = Page::withArchived()->findOrFail($id);
18
19
            // Can only delete a draft or archived page
20 5
            if (!$page->isDraft() && !$page->isArchived()) {
21 1
                return;
22
            }
23
24
            // Remove Page specific modules
25 4
            Module::where('page_id', $page->id)->delete();
26
27
            // Remove Page specific urls
28 4
            UrlRecord::getByModel($page)->each->delete();
29
30 4
            $page->delete();
31
32 4
            Audit::activity()
33 4
                ->performedOn($page)
34 4
                ->log('deleted');
35
36 4
            DB::commit();
37
        } catch (\Throwable $e) {
38
            DB::rollBack();
39
            throw $e;
40
        }
41 4
    }
42
}
43