Completed
Push — master ( 4d5a46...0c8810 )
by ARCANEDEV
09:08
created

Post   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 3
cbo 9
dl 0
loc 232
ccs 0
cts 78
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A scopePublished() 0 5 1
A scopePublishedAt() 0 5 1
A author() 0 7 1
A category() 0 4 1
A tags() 0 4 1
A setTitleAttribute() 0 5 1
A getStatusNameAttribute() 0 4 1
A createOne() 0 19 1
A updateOne() 0 14 2
A isDraft() 0 4 1
A isPublished() 0 4 1
A extractSeoAttributes() 0 9 1
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Arcanedev\LaravelSeo\Traits\Seoable;
4
use Arcanesoft\Blog\Entities\PostStatus;
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Str;
11
12
/**
13
 * Class     Post
14
 *
15
 * @package  Arcanesoft\Blog\Models
16
 * @author   ARCANEDEV <[email protected]>
17
 *
18
 * @property  int             id
19
 * @property  int             author_id
20
 * @property  int             category_id
21
 * @property  string          title
22
 * @property  string          slug
23
 * @property  string          excerpt
24
 * @property  string          content
25
 * @property  string          status
26
 * @property  \Carbon\Carbon  publish_date
27
 * @property  \Carbon\Carbon  created_at
28
 * @property  \Carbon\Carbon  updated_at
29
 * @property  \Carbon\Carbon  deleted_at
30
 *
31
 * @property  \Arcanesoft\Contracts\Auth\Models\User  user
32
 * @property  \Arcanesoft\Blog\Models\Category        category
33
 *
34
 * @method  static  \Illuminate\Database\Eloquent\Builder  published()
35
 * @method  static  \Illuminate\Database\Eloquent\Builder  publishedAt(int $year)
36
 */
37
class Post extends AbstractModel
38
{
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Traits
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    use Seoable, SoftDeletes;
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Properties
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * The database table used by the model
51
     *
52
     * @var string
53
     */
54
    protected $table = 'posts';
55
56
    /**
57
     * The attributes that are mass assignable
58
     *
59
     * @var array
60
     */
61
    protected $fillable = [
62
        'author_id', 'category_id', 'title', 'excerpt', 'content', 'status', 'publish_date'
63
    ];
64
65
    /**
66
     * The attributes that should be mutated to dates.
67
     *
68
     * @var array
69
     */
70
    protected $dates = ['publish_date', 'deleted_at'];
71
72
    /**
73
     * The attributes that should be casted to native types.
74
     *
75
     * @var array
76
     */
77
    protected $casts = [
78
        'author_id'   => 'integer',
79
        'category_id' => 'integer',
80
    ];
81
82
    /* ------------------------------------------------------------------------------------------------
83
     |  Scopes
84
     | ------------------------------------------------------------------------------------------------
85
     */
86
    /**
87
     * Scope only published posts.
88
     *
89
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
90
     */
91
    public function scopePublished(Builder $builder)
92
    {
93
        $builder->where('status', PostStatus::STATUS_PUBLISHED)
94
                ->where('publish_date', '<=', Carbon::now());
95
    }
96
97
    /**
98
     * Scope only published posts.
99
     *
100
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
101
     * @param  int                                    $year
102
     */
103
    public function scopePublishedAt(Builder $builder, $year)
104
    {
105
        $this->scopePublished($builder);
106
        $builder->where(DB::raw('YEAR(publish_date)'), $year);
107
    }
108
109
    /* ------------------------------------------------------------------------------------------------
110
     |  Relationships
111
     | ------------------------------------------------------------------------------------------------
112
     */
113
    /**
114
     * Author relationship.
115
     *
116
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
117
     */
118
    public function author()
119
    {
120
        return $this->belongsTo(
121
            config('auth.providers.users.model', \App\Models\User::class),
122
            'author_id'
123
        );
124
    }
125
126
    /**
127
     * Category relationship.
128
     *
129
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
130
     */
131
    public function category()
132
    {
133
        return $this->belongsTo(Category::class);
134
    }
135
136
    /**
137
     * Tags relationship.
138
     *
139
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
140
     */
141
    public function tags()
142
    {
143
        return $this->belongsToMany(Tag::class, "{$this->prefix}post_tag");
144
    }
145
146
    /* ------------------------------------------------------------------------------------------------
147
     |  Getters & Setters
148
     | ------------------------------------------------------------------------------------------------
149
     */
150
    /**
151
     * Set the title attribute.
152
     *
153
     * @param  string  $title
154
     */
155
    public function setTitleAttribute($title)
156
    {
157
        $this->attributes['title'] = $title;
158
        $this->attributes['slug']  = Str::slug($title);
159
    }
160
161
    /**
162
     * Get the status name attribute.
163
     *
164
     * @return string|null
165
     */
166
    public function getStatusNameAttribute()
167
    {
168
        return PostStatus::get($this->status);
169
    }
170
171
    /* ------------------------------------------------------------------------------------------------
172
     |  Main Functions
173
     | ------------------------------------------------------------------------------------------------
174
     */
175
    /**
176
     * Create a post.
177
     *
178
     * @param  array  $inputs
179
     *
180
     * @return bool
181
     */
182
    public function createOne(array $inputs)
183
    {
184
        $attributes = [
185
            'author_id'   => auth()->user()->getAuthIdentifier(),
186
            'category_id' => $inputs['category'],
187
        ] + Arr::only($inputs, [
188
            'title', 'excerpt', 'content', 'publish_date', 'status'
189
        ]);
190
191
        $this->fill($attributes);
192
        $saved = $this->save();
193
        $this->tags()->sync($inputs['tags']);
194
195
        $this->createSeo(
196
            $this->extractSeoAttributes($inputs)
197
        );
198
199
        return $saved;
200
    }
201
202
    /**
203
     * Create a post.
204
     *
205
     * @param  array  $inputs
206
     *
207
     * @return bool|int
208
     */
209
    public function updateOne(array $inputs)
210
    {
211
        $attributes = ['category_id' => $inputs['category']] + Arr::only($inputs, [
212
            'title', 'excerpt', 'content', 'publish_date', 'status'
213
        ]);
214
215
        $updated = $this->update($attributes);
216
        $this->tags()->sync($inputs['tags']);
217
218
        $seoAttributes = $this->extractSeoAttributes($inputs);
219
        $this->hasSeo() ? $this->updateSeo($seoAttributes) : $this->createSeo($seoAttributes);
220
221
        return $updated;
222
    }
223
224
    /* ------------------------------------------------------------------------------------------------
225
     |  Check Functions
226
     | ------------------------------------------------------------------------------------------------
227
     */
228
    /**
229
     * Check if the post's status is "draft".
230
     *
231
     * @return bool
232
     */
233
    public function isDraft()
234
    {
235
        return $this->status === PostStatus::STATUS_DRAFT;
236
    }
237
238
    /**
239
     * Check if the post's status is "published".
240
     *
241
     * @return bool
242
     */
243
    public function isPublished()
244
    {
245
        return $this->status === PostStatus::STATUS_PUBLISHED;
246
    }
247
248
    /* ------------------------------------------------------------------------------------------------
249
     |  Other Functions
250
     | ------------------------------------------------------------------------------------------------
251
     */
252
    /**
253
     * Extract the seo attributes.
254
     *
255
     * @param  array  $inputs
256
     *
257
     * @return array
258
     */
259
    protected function extractSeoAttributes(array $inputs)
260
    {
261
        return [
262
            'title'       => Arr::get($inputs, 'seo_title'),
263
            'description' => Arr::get($inputs, 'seo_description'),
264
            'keywords'    => Arr::get($inputs, 'seo_keywords'),
265
            'metas'       => Arr::get($inputs, 'seo_metas'),
266
        ];
267
    }
268
}
269