Completed
Push — master ( 5b04ab...a06757 )
by ARCANEDEV
07:06
created

Post::hasThumbnail()   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 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Arcanedev\LaravelSeo\Traits\Seoable;
4
use Carbon\Carbon;
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Str;
10
11
/**
12
 * Class     Post
13
 *
14
 * @package  Arcanesoft\Blog\Models
15
 * @author   ARCANEDEV <[email protected]>
16
 *
17
 * @property  int             id
18
 * @property  int             author_id
19
 * @property  int             category_id
20
 * @property  string          locale
21
 * @property  string          title
22
 * @property  string          slug
23
 * @property  string          excerpt
24
 * @property  string|null     thumbnail
25
 * @property  string          content_raw
26
 * @property  string          content_html
27
 * @property  bool            is_draft
28
 * @property  \Carbon\Carbon  published_at
29
 * @property  \Carbon\Carbon  created_at
30
 * @property  \Carbon\Carbon  updated_at
31
 * @property  \Carbon\Carbon  deleted_at
32
 *
33
 * @property  \Arcanesoft\Contracts\Auth\Models\User  user
34
 * @property  \Arcanesoft\Blog\Models\Category        category
35
 *
36
 * @method  static  \Illuminate\Database\Eloquent\Builder  published()
37
 * @method  static  \Illuminate\Database\Eloquent\Builder  publishedAt(int $year)
38
 * @method  static  \Illuminate\Database\Eloquent\Builder  localized(string|null $locale)
39
 */
40
class Post extends AbstractModel
41
{
42
    /* -----------------------------------------------------------------
43
     |  Constants
44
     | -----------------------------------------------------------------
45
     */
46
47
    const STATUS_DRAFT     = 'draft';
48
    const STATUS_PUBLISHED = 'published';
49
50
    /* -----------------------------------------------------------------
51
     |  Traits
52
     | -----------------------------------------------------------------
53
     */
54
55
    use Presenters\PostPresenter,
56
        Seoable,
57
        SoftDeletes;
58
59
    /* -----------------------------------------------------------------
60
     |  Properties
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * The database table used by the model
66
     *
67
     * @var string
68
     */
69
    protected $table = 'posts';
70
71
    /**
72
     * The attributes that are mass assignable
73
     *
74
     * @var array
75
     */
76
    protected $fillable = [
77
        'author_id', 'category_id', 'locale', 'title', 'excerpt', 'thumbnail', 'content', 'published_at',
78
    ];
79
80
    /**
81
     * The attributes that should be mutated to dates.
82
     *
83
     * @var array
84
     */
85
    protected $dates = ['published_at', 'deleted_at'];
86
87
    /**
88
     * The attributes that should be casted to native types.
89
     *
90
     * @var array
91
     */
92
    protected $casts = [
93
        'author_id'   => 'integer',
94
        'category_id' => 'integer',
95
        'is_draft'    => 'boolean',
96
    ];
97
98
    /* -----------------------------------------------------------------
99
     |  Scopes
100
     | -----------------------------------------------------------------
101
     */
102
103
    /**
104
     * Scope only published posts.
105
     *
106
     * @param  \Illuminate\Database\Eloquent\Builder  $query
107
     *
108
     * @return \Illuminate\Database\Eloquent\Builder
109
     */
110
    public function scopePublished(Builder $query)
111
    {
112
        return $query->where('is_draft', false)
113
                     ->where('published_at', '<=', Carbon::now());
114
    }
115
116
    /**
117
     * Scope only published posts.
118
     *
119
     * @param  \Illuminate\Database\Eloquent\Builder  $query
120
     * @param  int                                    $year
121
     *
122
     * @return \Illuminate\Database\Eloquent\Builder
123
     */
124
    public function scopePublishedAt(Builder $query, $year)
125
    {
126
        return $this->scopePublished($query)
127
                    ->where(DB::raw('YEAR(published_at)'), $year);
128
    }
129
130
    /**
131
     * Scope by post's locale.
132
     *
133
     * @param  \Illuminate\Database\Eloquent\Builder  $query
134
     * @param  string|null                            $locale
135
     *
136
     * @return \Illuminate\Database\Eloquent\Builder
137
     */
138
    public function scopeLocalized(Builder $query, $locale = null)
139
    {
140
        return $query->where('locale', $locale ?: config('app.locale'));
141
    }
142
143
    /* -----------------------------------------------------------------
144
     |  Relationships
145
     | -----------------------------------------------------------------
146
     */
147
148
    /**
149
     * Author relationship.
150
     *
151
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
152
     */
153
    public function author()
154
    {
155
        return $this->belongsTo(
156
            config('auth.providers.users.model', 'App\Models\User'),
157
            'author_id'
158
        );
159
    }
160
161
    /**
162
     * Category relationship.
163
     *
164
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
165
     */
166
    public function category()
167
    {
168
        return $this->belongsTo(Category::class);
169
    }
170
171
    /**
172
     * Tags relationship.
173
     *
174
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
175
     */
176
    public function tags()
177
    {
178
        return $this->belongsToMany(Tag::class, "{$this->prefix}post_tag");
179
    }
180
181
    /* -----------------------------------------------------------------
182
     |  Getters & Setters
183
     | -----------------------------------------------------------------
184
     */
185
186
    /**
187
     * Set the title attribute.
188
     *
189
     * @param  string  $title
190
     */
191
    public function setTitleAttribute($title)
192
    {
193
        $this->attributes['title'] = $title;
194
        $this->attributes['slug']  = Str::slug($title);
195
    }
196
197
    /**
198
     * Set the content attribute.
199
     *
200
     * @param  string  $content
201
     */
202
    public function setContentAttribute($content)
203
    {
204
        $this->attributes['content_raw']  = $content;
205
        $this->attributes['content_html'] = markdown($content);
206
    }
207
208
    /* -----------------------------------------------------------------
209
     |  Main Functions
210
     | -----------------------------------------------------------------
211
     */
212
213
    /**
214
     * Create a post.
215
     *
216
     * @param  array  $attributes
217
     *
218
     * @return self
219
     */
220
    public static function createOne(array $attributes)
221
    {
222
        $post = new self($attributes);
223
        $post->save();
224
225
        $post->tags()->sync($attributes['tags']);
226
227
        $post->createSeo(
228
            static::extractSeoAttributes($attributes)
229
        );
230
231
        return $post;
232
    }
233
234
    /**
235
     * Create a post.
236
     *
237
     * @param  array  $inputs
238
     *
239
     * @return bool|int
240
     */
241
    public function updateOne(array $inputs)
242
    {
243
        $updated = $this->setStatusAttribute($inputs['status'])
244
            ->update(Arr::except($inputs, ['author_id']));
245
246
        $this->tags()->sync($inputs['tags']);
247
248
        $seoAttributes = static::extractSeoAttributes($inputs);
249
250
        $this->hasSeo() ? $this->updateSeo($seoAttributes) : $this->createSeo($seoAttributes);
251
252
        return $updated;
253
    }
254
255
    /* -----------------------------------------------------------------
256
     |  Check Functions
257
     | -----------------------------------------------------------------
258
     */
259
260
    /**
261
     * Check if the post's status is "draft".
262
     *
263
     * @return bool
264
     */
265
    public function isDraft()
266
    {
267
        return $this->is_draft;
268
    }
269
270
    /**
271
     * Check if the post's status is "published".
272
     *
273
     * @return bool
274
     */
275
    public function isPublished()
276
    {
277
        return ! $this->isDraft();
278
    }
279
280
    /**
281
     * Check if the post has thumbnail.
282
     *
283
     * @return bool
284
     */
285
    public function hasThumbnail()
286
    {
287
        return ! is_null($this->thumbnail);
288
    }
289
290
    /* -----------------------------------------------------------------
291
     |  Other Methods
292
     | -----------------------------------------------------------------
293
     */
294
295
    /**
296
     * Extract the seo attributes.
297
     *
298
     * @param  array  $inputs
299
     *
300
     * @return array
301
     */
302
    protected static function extractSeoAttributes(array $inputs)
303
    {
304
        return [
305
            'title'       => Arr::get($inputs, 'seo_title'),
306
            'description' => Arr::get($inputs, 'seo_description'),
307
            'keywords'    => Arr::get($inputs, 'seo_keywords'),
308
            'metas'       => Arr::get($inputs, 'seo_metas'),
309
        ];
310
    }
311
312
    /**
313
     * Get the show url.
314
     *
315
     * @return string
316
     */
317
    public function getShowUrl()
318
    {
319
        return route('admin::blog.posts.show', [$this]);
320
    }
321
322
    /**
323
     * Get the edit url.
324
     *
325
     * @return string
326
     */
327
    public function getEditUrl()
328
    {
329
        return route('admin::blog.posts.edit', [$this]);
330
    }
331
}
332