Passed
Pull Request — master (#95)
by Fèvre
10:51 queued 05:22
created

DiscussPostObserver::deleting()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
nc 12
nop 1
dl 0
loc 20
rs 9.6111
c 1
b 0
f 0
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
        if (is_null($discussPost->user_id)) {
0 ignored issues
show
introduced by
The condition is_null($discussPost->user_id) is always false.
Loading history...
19
            $discussPost->user_id = Auth::id();
20
        }
21
    }
22
23
    /**
24
     * Handle the "deleting" event.
25
     */
26
    public function deleting(DiscussPost $discussPost): void
27
    {
28
        $conversation = $discussPost->conversation;
29
30
        if ($conversation->first_post_id == $discussPost->getKey()) {
31
            $conversation->delete();
32
        }
33
34
        if ($conversation->last_post_id == $discussPost->getKey()) {
35
            $previousPost = DiscussPostRepository::findPreviousPost($discussPost, true);
36
37
            $conversation->last_post_id = !is_null($previousPost) ? $previousPost->getKey() : null;
38
        }
39
40
        if ($conversation->solved_post_id == $discussPost->getKey()) {
41
            $conversation->solved_post_id = null;
42
            $conversation->is_solved = false;
43
        }
44
45
        $conversation->save();
46
    }
47
}
48