Passed
Push — 5.0.0 ( 49e1c0...87aae2 )
by Fèvre
06:22
created

BlogArticleObserver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 26
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updating() 0 3 1
A creating() 0 3 1
A deleting() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Observers;
6
7
use Illuminate\Support\Facades\Auth;
8
use Xetaravel\Models\BlogArticle;
9
10
class BlogArticleObserver
11
{
12
    /**
13
     * Handle the "creating" event.
14
     */
15
    public function creating(BlogArticle $blogArticle): void
16
    {
17
        $blogArticle->user_id = Auth::id();
18
    }
19
20
    /**
21
     * Handle the "updating" event.
22
     */
23
    public function updating(BlogArticle $blogArticle): void
24
    {
25
        $blogArticle->generateSlug();
26
    }
27
28
    /**
29
     * Handle the "deleting" event.
30
     */
31
    public function deleting(BlogArticle $blogArticle): void
32
    {
33
        // We need to do this to refresh the countable cache `blog_comment_count` of the user.
34
        foreach ($blogArticle->comments as $comment) {
35
            $comment->delete();
36
        }
37
    }
38
}
39