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

DeletePage::handle()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.1574

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 25
ccs 11
cts 14
cp 0.7856
rs 9.7998
c 0
b 0
f 0
cc 4
nc 9
nop 1
crap 4.1574
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