Completed
Pull Request — master (#10)
by ARCANEDEV
06:56
created

Tag::createMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Arcanedev\Localization\Traits\HasTranslations;
4
use Arcanesoft\Blog\Blog;
5
use Arcanesoft\Blog\Events\Tags as TagEvents;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
10
/**
11
 * Class     Category
12
 *
13
 * @package  Arcanesoft\Blog\Models
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @property  int             id
17
 * @property  string          name
18
 * @property  string          slug
19
 * @property  \Carbon\Carbon  created_at
20
 * @property  \Carbon\Carbon  updated_at
21
 * @property  \Carbon\Carbon  deleted_at
22
 *
23
 * @property  \Illuminate\Database\Eloquent\Collection  posts
24
 */
25
class Tag extends AbstractModel
26
{
27
    /* -----------------------------------------------------------------
28
     |  Constants
29
     | -----------------------------------------------------------------
30
     */
31
32
    const SELECT_CACHE_NAME = 'blog::tags.select-data';
33
34
    /* -----------------------------------------------------------------
35
     |  Traits
36
     | -----------------------------------------------------------------
37
     */
38
39
    use SoftDeletes,
40
        HasTranslations;
41
42
    /* -----------------------------------------------------------------
43
     |  Properties
44
     | -----------------------------------------------------------------
45
     */
46
47
    /**
48
     * The database table used by the model
49
     *
50
     * @var string
51
     */
52
    protected $table = 'tags';
53
54
    /**
55
     * The attributes that are mass assignable
56
     *
57
     * @var array
58
     */
59
    protected $fillable = ['name', 'slug'];
60
61
    /**
62
     * The attributes that should be mutated to dates.
63
     *
64
     * @var array
65
     */
66
    protected $dates = ['deleted_at'];
67
68
    /**
69
     * The event map for the model.
70
     *
71
     * @var array
72
     */
73
    protected $dispatchesEvents = [
74
        'creating'  => TagEvents\TagCreating::class,
75
        'created'   => TagEvents\TagCreated::class,
76
        'updating'  => TagEvents\TagUpdating::class,
77
        'updated'   => TagEvents\TagUpdated::class,
78
        'saving'    => TagEvents\TagSaving::class,
79
        'saved'     => TagEvents\TagSaved::class,
80
        'deleting'  => TagEvents\TagDeleting::class,
81
        'deleted'   => TagEvents\TagDeleted::class,
82
        'restoring' => TagEvents\TagRestoring::class,
83
        'restored'  => TagEvents\TagRestored::class,
84
    ];
85
86
    /* -----------------------------------------------------------------
87
     |  Relationships
88
     | -----------------------------------------------------------------
89
     */
90
91
    /**
92
     * Posts relationship.
93
     *
94
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
95
     */
96 2
    public function posts()
97
    {
98 2
        return $this->belongsToMany(Post::class, $this->getPrefix().'post_tag');
99
    }
100
101
    /* -----------------------------------------------------------------
102
     |  Getters & Setters
103
     | -----------------------------------------------------------------
104
     */
105
106
    /**
107
     * Set the name attribute.
108
     *
109
     * @param  string  $name
110
     *
111
     * @return string
112
     */
113 12
    public function setNameAttribute($name)
114
    {
115 12
        return $this->attributes['name'] = $name;
116
    }
117
118
    /**
119
     * Set the slug attribute.
120
     *
121
     * @param  string  $name
122
     *
123
     * @return string
124
     */
125 12
    public function setSlugAttribute($name)
126
    {
127 12
        return $this->attributes['slug'] = Str::slug($name);
128
    }
129
130
    /**
131
     * Get the translatable attributes.
132
     *
133
     * @return array
134
     */
135 12
    public function getTranslatableAttributes()
136
    {
137 12
        return Blog::instance()->isTranslatable() ? ['name', 'slug'] : [];
138
    }
139
140
    /* -----------------------------------------------------------------
141
     |  Main Methods
142
     | -----------------------------------------------------------------
143
     */
144
145
    /**
146
     * Create a new tag.
147
     *
148
     * @param  array  $attributes
149
     *
150
     * @return self
151
     */
152
    public static function createOne(array $attributes)
153
    {
154 12
        return tap(new self, function (self $category) use ($attributes) {
155 12
            $attributes['slug'] = $attributes['slug'] ?? $attributes['name'];
156
157 12
            $category->populate($attributes)->save();
158 12
        });
159
    }
160
161
    /**
162
     * Create many tags.
163
     *
164
     * @param  array  $tags
165
     *
166
     * @return \Illuminate\Support\Collection
167
     */
168
    public static function createMany(array $tags)
169
    {
170 2
        return collect($tags)->transform(function ($attributes) {
171 2
            return static::createOne($attributes);
172 2
        });
173
    }
174
175
    /**
176
     * Update the current tag.
177
     *
178
     * @param  array  $attributes
179
     *
180
     * @return self
181
     */
182 4
    public function updateOne(array $attributes)
183
    {
184 4
        $this->populate($attributes)->save();
185
186 4
        return $this;
187
    }
188
189
    /**
190
     * Get the categories options for select input.
191
     *
192
     * @return \Illuminate\Support\Collection
193
     */
194 2
    public static function getSelectData()
195
    {
196 2
        $minutes = config('arcanesoft.blog.cache.tags.select-data', 5);
197
198
        /** @var  \Illuminate\Database\Eloquent\Collection  $categories */
199 2
        return cache()->remember(self::SELECT_CACHE_NAME, $minutes, function () {
200 2
            $withTranslations = Blog::instance()->isTranslatable();
201
202 2
            return self::all()->mapWithKeys(function (Tag $tag) use ($withTranslations) {
203
                return [
204 2
                    $tag->id => $withTranslations
205
                        ? implode(' / ', $tag->getTranslations('name'))
206 2
                        : $tag->name
207
                ];
208 2
            });
209 2
        })->toBase();
210
    }
211
212
    /* -----------------------------------------------------------------
213
     |  Check Methods
214
     | -----------------------------------------------------------------
215
     */
216
217
    /**
218
     * Check if tag has posts.
219
     *
220
     * @return bool
221
     */
222 2
    public function hasPosts()
223
    {
224 2
        return ! $this->posts->isEmpty();
225
    }
226
227
    /**
228
     * Check if the category is deletable.
229
     *
230
     * @return bool
231
     */
232 2
    public function isDeletable()
233
    {
234 2
        return ! $this->hasPosts();
235
    }
236
237
    /* -----------------------------------------------------------------
238
     |  Other Methods
239
     | -----------------------------------------------------------------
240
     */
241
242
    /**
243
     * Fill the model with an array of attributes.
244
     *
245
     * @param  array  $attributes
246
     *
247
     * @return self
248
     */
249 12
    protected function populate(array $attributes)
250
    {
251 12
        if ( ! Blog::instance()->isTranslatable())
252 8
            return $this->fill($attributes);
253
254 4
        $keys = ['name', 'slug'];
255
256 4
        foreach ($keys as $key) {
257 4
            $this->setTranslations($key, $attributes[$key] ?? []);
258
        }
259
260 4
        return $this->fill(Arr::except($attributes, $keys));
261
    }
262
}
263