Completed
Push — master ( 392bd3...60208d )
by ARCANEDEV
04:52
created

Post::isPublished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Arcanesoft\Blog\Bases\Model;
4
use Arcanesoft\Blog\Entities\PostStatus;
5
use Illuminate\Database\Eloquent\SoftDeletes;
6
7
/**
8
 * Class     Post
9
 *
10
 * @package  Arcanesoft\Blog\Models
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @property  int             id
14
 * @property  int             author_id
15
 * @property  int             category_id
16
 * @property  string          title
17
 * @property  string          slug
18
 * @property  string          excerpt
19
 * @property  string          content
20
 * @property  string          status
21
 * @property  \Carbon\Carbon  publish_date
22
 * @property  \Carbon\Carbon  created_at
23
 * @property  \Carbon\Carbon  updated_at
24
 * @property  \Carbon\Carbon  deleted_at
25
 *
26
 * @property  \Arcanesoft\Contracts\Auth\Models\User  user
27
 * @property  \Arcanesoft\Blog\Models\Category        category
28
 */
29
class Post extends Model
30
{
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Traits
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    use SoftDeletes;
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Properties
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * The database table used by the model
43
     *
44
     * @var string
45
     */
46
    protected $table = 'posts';
47
48
    /**
49
     * The attributes that are mass assignable
50
     *
51
     * @var array
52
     */
53
    protected $fillable = [
54
        'author_id', 'category_id', 'title', 'excerpt', 'content', 'status', 'publish_date'
55
    ];
56
57
    /**
58
     * Set or unset the timestamps for the model
59
     *
60
     * @var bool
61
     */
62
    public $timestamps = true;
63
64
    /**
65
     * The attributes that should be mutated to dates.
66
     *
67
     * @var array
68
     */
69
    protected $dates = ['publish_date', 'deleted_at'];
70
71
    /**
72
     * The attributes that should be casted to native types.
73
     *
74
     * @var array
75
     */
76
    protected $casts = [
77
        'author_id'   => 'integer',
78
        'category_id' => 'integer',
79
    ];
80
81
    /* ------------------------------------------------------------------------------------------------
82
     |  Relationships
83
     | ------------------------------------------------------------------------------------------------
84
     */
85
    /**
86
     * Relationship with author.
87
     *
88
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
89
     */
90
    public function author()
91
    {
92
        return $this->belongsTo(config('auth.model'), 'author_id');
93
    }
94
95
    /**
96
     * Relationship with category.
97
     *
98
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
99
     */
100
    public function category()
101
    {
102
        return $this->belongsTo(Category::class);
103
    }
104
105
    /**
106
     * Relationship with tags.
107
     *
108
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
109
     */
110
    public function tags()
111
    {
112
        return $this->belongsToMany(Tag::class, $this->prefix . 'post_tag');
113
    }
114
115
    /* ------------------------------------------------------------------------------------------------
116
     |  Getters & Setters
117
     | ------------------------------------------------------------------------------------------------
118
     */
119
    /**
120
     * Set the title attribute.
121
     *
122
     * @param  string  $title
123
     */
124
    public function setTitleAttribute($title)
125
    {
126
        $this->attributes['title'] = $title;
127
        $this->attributes['slug']  = str_slug($title);
128
    }
129
130
    /**
131
     * Get the status name attribute.
132
     *
133
     * @return null|string
134
     */
135
    public function getStatusNameAttribute()
136
    {
137
        return PostStatus::get($this->status);
138
    }
139
140
    /* ------------------------------------------------------------------------------------------------
141
     |  Main Functions
142
     | ------------------------------------------------------------------------------------------------
143
     */
144
    /**
145
     * Create a post.
146
     *
147
     * @param  array  $inputs
148
     *
149
     * @return bool
150
     */
151
    public function createOne(array $inputs)
152
    {
153
        $attributes = [
154
            'author_id'   => auth()->user()->getAuthIdentifier(),
155
            'category_id' => $inputs['category'],
156
        ] + array_only($inputs, [
157
            'title', 'excerpt', 'content', 'publish_date', 'status'
158
        ]);
159
160
        $this->fill($attributes);
161
        $saved = $this->save();
162
        $this->tags()->sync($inputs['tags']);
163
164
        return $saved;
165
    }
166
167
    /* ------------------------------------------------------------------------------------------------
168
     |  Check Functions
169
     | ------------------------------------------------------------------------------------------------
170
     */
171
    /**
172
     * Check if the post's status is "draft".
173
     *
174
     * @return bool
175
     */
176
    public function isDraft()
177
    {
178
        return $this->status === PostStatus::STATUS_DRAFT;
179
    }
180
181
    /**
182
     * Check if the post's status is "published".
183
     *
184
     * @return bool
185
     */
186
    public function isPublished()
187
    {
188
        return $this->status === PostStatus::STATUS_PUBLISHED;
189
    }
190
}
191