Completed
Push — master ( c72a1f...5e0e31 )
by Christopher
01:10
created

Post::disapprovedComments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Chriscreates\Blog;
4
5
use Carbon\Carbon;
6
use Chriscreates\Blog\Builders\PostBuilder;
7
use Chriscreates\Blog\Traits\IsAuthorable;
8
use Chriscreates\Blog\Traits\Post\PostAttributes;
9
use Chriscreates\Blog\Traits\Post\PostsHaveACategory;
10
use Chriscreates\Blog\Traits\Post\PostsHaveComments;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Support\Str;
13
14
class Post extends Model
15
{
16
    use PostAttributes,
17
    IsAuthorable,
18
    PostsHaveComments,
19
    PostsHaveACategory;
20
21
    const PUBLISHED = 'published';
22
    const DRAFT = 'draft';
23
    const SCHEDULED = 'scheduled';
24
25
    protected $table = 'posts';
26
27
    protected $primaryKey = 'id';
28
29
    public $guarded = ['id'];
30
31
    public $timestamps = true;
32
33
    protected $appends = ['tagsCount'];
34
35
    protected $dates = ['published_at'];
36
37
    public function category()
38
    {
39
        return $this->hasOne(Category::class, 'id', 'category_id');
40
    }
41
42
    public function comments()
43
    {
44
        if ( ! $this->allow_comments) {
45
            return null;
46
        }
47
48
        if ( ! $this->allow_guest_comments) {
49
            return $this->morphMany(Comment::class, 'commentable')
50
            ->whereNull('user_id');
51
        }
52
53
        return $this->morphMany(Comment::class, 'commentable');
54
    }
55
56
    public function approvedComments()
57
    {
58
        return $this->comments()->where('is_approved', true);
59
    }
60
61
    public function disapprovedComments()
62
    {
63
        return $this->comments()->where('is_approved', false);
64
    }
65
66
    public function guestComments()
67
    {
68
        return $this->comments()->whereNull('user_id');
69
    }
70
71
    public function userComments()
72
    {
73
        return $this->comments()->whereNotNull('user_id');
74
    }
75
76
    public function tags()
77
    {
78
        return $this->morphToMany(Tag::class, 'taggable');
79
    }
80
81
    public function newEloquentBuilder($query) : PostBuilder
82
    {
83
        return new PostBuilder($query);
84
    }
85
86
    public function path()
87
    {
88
        return "/posts/{$this->slug}";
89
    }
90
91
    public function setPublished()
92
    {
93
        $this->status = self::PUBLISHED;
94
        $this->published_at = now();
95
96
        return $this;
97
    }
98
99
    public function setDrafted()
100
    {
101
        $this->status = self::DRAFT;
102
        $this->published_at = null;
103
104
        return $this;
105
    }
106
107
    public function setScheduled($publish_date = null)
108
    {
109
        if (is_null($publish_date)) {
110
            $publish_date = request('published_at');
111
        }
112
113
        $this->status = self::SCHEDULED;
114
        $this->published_at = new Carbon($publish_date);
115
116
        return $this;
117
    }
118
119
    public function setStatus(string $status = null)
120
    {
121
        if (is_null($status)) {
122
            $status = request('status');
123
        }
124
125
        if ($status == self::PUBLISHED) {
126
            $this->setPublished();
127
        }
128
129
        if ($status == self::DRAFT) {
130
            $this->setDrafted();
131
        }
132
133
        if ($status == self::SCHEDULED) {
134
            $this->setScheduled(request('published_at'));
135
        }
136
137
        return $this;
138
    }
139
140
    public function setSlug(string $title = null)
141
    {
142
        $set_slug = ! is_null($title) ? $title : $this->title;
143
144
        $this->slug = Str::slug($set_slug);
145
146
        return $this;
147
    }
148
149
    public function statuses()
150
    {
151
        return collect([
152
            self::PUBLISHED,
153
            self::DRAFT,
154
            self::SCHEDULED,
155
        ]);
156
    }
157
}
158