Comment   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A author() 0 9 2
A post() 0 3 1
A boot() 0 5 1
A user() 0 3 1
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Models;
4
5
use App\User;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
use WebDevEtc\BlogEtc\Scopes\BlogCommentApprovedAndDefaultOrderScope;
9
10
class Comment extends Model
11
{
12
    public $casts = [
13
        'approved' => 'boolean',
14
    ];
15
16
    public $fillable = [
17
        'comment',
18
        'author_name',
19
    ];
20
21
    protected $table = 'blog_etc_comments';
22
23
    /**
24
     * The "booting" method of the model.
25
     *
26
     * @return void
27
     */
28
    protected static function boot()
29
    {
30
        parent::boot();
31
32
        static::addGlobalScope(new BlogCommentApprovedAndDefaultOrderScope());
33
    }
34
35
    /**
36
     * The associated Post for this comment..
37
     *
38
     * @return BelongsTo
39
     */
40
    public function post(): BelongsTo
41
    {
42
        return $this->belongsTo(Post::class, 'blog_etc_post_id');
43
    }
44
45
    /**
46
     * Comment author user (if set).
47
     *
48
     * @return BelongsTo
49
     */
50
    public function user(): BelongsTo
51
    {
52
        return $this->belongsTo(User::class);
53
    }
54
55
    /**
56
     * Return author string (either from the User (via ->user_id), or the submitted author_name value.
57
     *
58
     * @return string
59
     */
60
    public function author()
61
    {
62
        if ($this->user_id) {
63
            $field = config('blogetc.comments.user_field_for_author_name', 'name');
64
65
            return optional($this->user)->$field;
66
        }
67
68
        return $this->author_name;
69
    }
70
}
71