Completed
Pull Request — master (#274)
by
unknown
63:55 queued 33:13
created

DeletePage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 27
ccs 11
cts 14
cp 0.7856
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 25 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
9
class DeletePage
10
{
11 3
    public function handle($id)
12
    {
13
        try {
14 3
            DB::beginTransaction();
15
16 3
            $page = Page::withArchived()->findOrFail($id);
17
18
            // Can only delete a draft or archived page
19 3
            if (!$page->isDraft() && !$page->isArchived()) {
20 1
                return;
21
            }
22
23
            // Remove Page specific modules
24 2
            Module::where('page_id', $page->id)->delete();
25
26 2
            $page->delete();
27
28 2
            Audit::activity()
29 2
                ->performedOn($page)
30 2
                ->log('deleted');
31
            
32 2
            DB::commit();
33
        } catch (\Throwable $e) {
34
            DB::rollBack();
35
            throw $e;
36
        }
37 2
    }
38
}
39