DiscussPostObserver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 44
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updating() 0 5 1
A creating() 0 3 1
A deleting() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Observers;
6
7
use Illuminate\Support\Facades\Auth;
8
use Xetaravel\Models\DiscussPost;
9
use Xetaravel\Models\Repositories\DiscussPostRepository;
10
11
class DiscussPostObserver
12
{
13
    /**
14
 * Handle the "creating" event.
15
 */
16
    public function creating(DiscussPost $discussPost): void
17
    {
18
        $discussPost->user_id = Auth::id();
19
    }
20
21
    /**
22
     * Handle the "updating" event.
23
     */
24
    public function updating(DiscussPost $discussPost): void
25
    {
26
        $discussPost->edited_user_id = Auth::id();
27
        $discussPost->is_edited = true;
28
        $discussPost->edit_count++;
29
    }
30
31
    /**
32
     * Handle the "deleting" event.
33
     */
34
    public function deleting(DiscussPost $discussPost): void
35
    {
36
        $discussPost->loadMissing('conversation');
37
        $conversation = $discussPost->conversation;
38
39
        /*if ($conversation->first_post_id === $discussPost->getKey()) {
40
            $conversation->delete();
41
        }*/
42
43
        if ($conversation->last_post_id === $discussPost->getKey()) {
44
            $previousPost = DiscussPostRepository::findPreviousPost($discussPost, true);
45
46
            $conversation->last_post_id = !is_null($previousPost) ? $previousPost->getKey() : null;
47
        }
48
49
        if ($conversation->solved_post_id === $discussPost->getKey()) {
50
            $conversation->solved_post_id = null;
51
            $conversation->is_solved = false;
52
        }
53
54
        $conversation->save();
55
    }
56
}
57