PostObserver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B saving() 0 22 6
A deleting() 0 8 2
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