Completed
Pull Request — master (#6)
by ARCANEDEV
05:48
created

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