Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

Comment::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
/**
9
 * @property int $id
10
 * @property User $user
11
 * @property int $user_id
12
 * @property int $parent_id
13
 * @property string $email
14
 * @property string $text
15
 * @property \Coyote\Comment[]|\Illuminate\Support\Collection $children
16
 * @property Comment $parent
17
 * @property string $html
18
 * @property \Coyote\Job|\Coyote\Guide $resource
19
 * @property string $resource_type
20
 * @property int $resource_id
21
 */
22
class Comment extends Model
23
{
24
    use SoftDeletes;
25
26
    /**
27
     * @var string[]
28
     */
29
    protected $fillable = ['user_id', 'email', 'parent_id', 'text'];
30
31
    /**
32
     * @var string
33
     */
34
    protected $table = 'comments';
35
36
    /**
37
     * @var array
38
     */
39
    protected $appends = ['html'];
40
41
    /**
42
     * @var null|string
43
     */
44
    private $html = null;
45
46
    public function resource()
47
    {
48
        return $this->morphTo();
49
    }
50
51
    /**
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
53
     */
54
    public function user()
55
    {
56
        return $this->belongsTo(User::class);
57
    }
58
59
    public function guide()
60
    {
61
        return $this->belongsTo(Guide::class);
62
    }
63
64
    public function children()
65
    {
66
        return $this->hasMany(Comment::class, 'parent_id', 'id');
67
    }
68
69
    public function parent()
70
    {
71
        return $this->belongsTo(Comment::class);
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getHtmlAttribute()
78
    {
79
        if ($this->html !== null) {
80
            return $this->html;
81
        }
82
83
        return $this->html = app('parser.post')->parse($this->text);
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function routeNotificationForMail()
90
    {
91
        return $this->email ?: $this->user->email;
92
    }
93
}
94