Completed
Push — master ( 5b4baf...4a3f73 )
by ARCANEDEV
04:07
created

Category::isDeletable()   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 Illuminate\Database\Eloquent\SoftDeletes;
4
use Illuminate\Support\Facades\Cache;
5
use Illuminate\Support\Str;
6
7
/**
8
 * Class     Category
9
 *
10
 * @package  Arcanesoft\Blog\Models
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @property  int             id
14
 * @property  string          name
15
 * @property  string          slug
16
 * @property  \Carbon\Carbon  created_at
17
 * @property  \Carbon\Carbon  updated_at
18
 * @property  \Carbon\Carbon  deleted_at
19
 *
20
 * @property  \Illuminate\Database\Eloquent\Collection  posts
21
 */
22
class Category extends AbstractModel
23
{
24
    /* -----------------------------------------------------------------
25
     |  Traits
26
     | -----------------------------------------------------------------
27
     */
28
29
    use SoftDeletes;
30
31
    /* -----------------------------------------------------------------
32
     |  Properties
33
     | -----------------------------------------------------------------
34
     */
35
36
    /**
37
     * The database table used by the model
38
     *
39
     * @var string
40
     */
41
    protected $table    = 'categories';
42
43
    /**
44
     * The attributes that are mass assignable
45
     *
46
     * @var array
47
     */
48
    protected $fillable = ['name'];
49
50
    /**
51
     * The attributes that should be mutated to dates.
52
     *
53
     * @var array
54
     */
55
    protected $dates    = ['deleted_at'];
56
57
    /**
58
     * The attributes that should be cast to native types.
59
     *
60
     * @var array
61
     */
62
    protected $casts = [
63
        'id' => 'integer',
64
    ];
65
66
    /* -----------------------------------------------------------------
67
     |  Relationships
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Relationship with posts.
73
     *
74
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
75
     */
76
    public function posts()
77
    {
78
        return $this->hasMany(Post::class);
79
    }
80
81
    /* -----------------------------------------------------------------
82
     |  Getters & Setters
83
     | -----------------------------------------------------------------
84
     */
85
86
    /**
87
     * Set the name attribute.
88
     *
89
     * @param  string  $name
90
     */
91
    public function setNameAttribute($name)
92
    {
93
        $this->attributes['name'] = $name;
94
        $this->attributes['slug'] = Str::slug($name);
95
    }
96
97
    /* -----------------------------------------------------------------
98
     |  Main Methods
99
     | -----------------------------------------------------------------
100
     */
101
102
    /**
103
     * Get the categories options for select input.
104
     *
105
     * @param  bool  $placeholder
106
     *
107
     * @return \Illuminate\Database\Eloquent\Collection
108
     */
109
    public static function getSelectOptions($placeholder = true)
110
    {
111
        /** @var  \Illuminate\Database\Eloquent\Collection  $categories */
112
        $categories = Cache::remember('blog_categories_select_options', 5, function () {
113
            return self::all()->pluck('name', 'id');
114
        });
115
116
        return $placeholder
117
            ? $categories->prepend(trans('blog::categories.select-category'), 0)
118
            : $categories;
119
    }
120
121
    /* -----------------------------------------------------------------
122
     |  Check Methods
123
     | -----------------------------------------------------------------
124
     */
125
126
    /**
127
     * Check if category has posts.
128
     *
129
     * @return bool
130
     */
131
    public function hasPosts()
132
    {
133
        return ! $this->posts->isEmpty();
134
    }
135
136
    /**
137
     * Check if the category is deletable.
138
     *
139
     * @return bool
140
     */
141
    public function isDeletable()
142
    {
143
        return ! $this->hasPosts();
144
    }
145
146
    /* -----------------------------------------------------------------
147
     |  Other Methods
148
     | -----------------------------------------------------------------
149
     */
150
151
    /**
152
     * Clear the cached categories.
153
     */
154
    public static function clearCache()
155
    {
156
        cache()->forget('blog_categories_select_options');
157
    }
158
}
159