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

DeletePage::handle()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.128

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 28
ccs 12
cts 15
cp 0.8
rs 9.7666
cc 4
nc 10
nop 1
crap 4.128
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