PostObserver::saving()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
cc 6
nc 32
nop 1
1
<?php
2
3
namespace Chriscreates\Blog\Observers;
4
5
use Chriscreates\Blog\Post;
6
use Illuminate\Support\Facades\Auth;
7
8
class PostObserver
9
{
10
    /**
11
     * Handle the post "saving" event.
12
     *
13
     * @param  \Chriscreates\Blog\Post  $post
14
     * @return void
15
     */
16
    public function saving(Post $post)
17
    {
18
        if (request()->filled('tags')) {
19
            $post->tags()->sync(request('tags'));
20
        }
21
22
        if (request()->filled('category_id')) {
23
            $post->category_id = request('category_id');
24
        }
25
26
        if ( ! request()->filled('user_id')) {
27
            $post->user_id = Auth::id();
28
        }
29
30
        if ( ! request()->filled('slug')) {
31
            $post->setSlug(request('title'));
32
        }
33
34
        if (request()->filled('status')) {
35
            $post->setStatus(request('status'));
36
        }
37
    }
38
39
    /**
40
     * Handle the post "deleting" event.
41
     *
42
     * @param  \Chriscreates\Blog\Post  $post
43
     * @return void
44
     */
45
    public function deleting(Post $post)
46
    {
47
        $post->tags()->detach();
48
49
        if ($post->comments()) {
50
            $post->comments()->delete();
51
        }
52
    }
53
}
54