Completed
Push — fix-various-bugs ( de2bba )
by Fèvre
02:31
created

DiscussPost::boot()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Models;
3
4
use Eloquence\Behaviours\CountCache\Countable;
5
use Illuminate\Support\Facades\Auth;
6
use Xetaio\Mentions\Models\Traits\HasMentionsTrait;
7
use Xetaravel\Models\Gates\FloodGate;
8
use Xetaravel\Models\Presenters\DiscussPostPresenter;
9
use Xetaravel\Models\Repositories\DiscussPostRepository;
10
11
class DiscussPost extends Model
12
{
13
    use Countable,
14
        DiscussPostPresenter,
15
        FloodGate,
16
        HasMentionsTrait;
17
18
    /**
19
     * The attributes that are mass assignable.
20
     *
21
     * @var array
22
     */
23
    protected $fillable = [
24
        'user_id',
25
        'conversation_id',
26
        'content',
27
        'is_edited'
28
    ];
29
30
    /**
31
     * The accessors to append to the model's array form.
32
     *
33
     * @var array
34
     */
35
    protected $appends = [
36
        'content_markdown',
37
        'post_url'
38
    ];
39
40
    /**
41
     * The attributes that should be mutated to dates.
42
     *
43
     * @var array
44
     */
45
    protected $dates = [
46
        'edited_at'
47
    ];
48
49
    /**
50
     * The "booting" method of the model.
51
     *
52
     * @return void
53
     */
54
    protected static function boot()
55
    {
56
        parent::boot();
57
58
        // Set the user id to the new post before saving it.
59
        static::creating(function ($model) {
60
            $model->user_id = Auth::id();
61
        });
62
63
        static::deleting(function ($model) {
64
            $conversation = $model->conversation;
65
66
            if ($conversation->first_post_id == $model->getKey()) {
67
                $conversation->delete();
68
            }
69
70
            if ($conversation->last_post_id == $model->getKey()) {
71
                $previousPost = DiscussPostRepository::findPreviousPost($model, true);
72
73
                $conversation->last_post_id = !is_null($previousPost) ? $previousPost->getKey() : null;
74
            }
75
76
            if ($conversation->solved_post_id == $model->getKey()) {
77
                $conversation->solved_post_id = null;
78
                $conversation->is_solved = false;
79
            }
80
81
            $conversation->save();
82
        });
83
    }
84
85
    /**
86
     * Return the count cache configuration.
87
     *
88
     * @return array
89
     */
90
    public function countCaches(): array
91
    {
92
        return [
93
            'discuss_post_count' => [User::class, 'user_id', 'id'],
94
            'post_count' => [DiscussConversation::class, 'conversation_id', 'id']
95
        ];
96
    }
97
98
    /**
99
     * Get the user that owns the post.
100
     *
101
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
102
     */
103
    public function user()
104
    {
105
        return $this->belongsTo(User::class);
106
    }
107
108
    /**
109
     * Get the conversation that owns the post.
110
     *
111
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
112
     */
113
    public function conversation()
114
    {
115
        return $this->belongsTo(DiscussConversation::class);
116
    }
117
118
    /**
119
     * Get the user that edited the post.
120
     *
121
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
122
     */
123
    public function editedUser()
124
    {
125
        return $this->hasOne(User::class, 'id', 'edited_user_id');
126
    }
127
}
128