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

Tag::updateOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php namespace Arcanesoft\Blog\Models;
2
3
use Arcanesoft\Blog\Blog;
4
use Arcanesoft\Blog\Events\Tags as TagEvents;
5
6
/**
7
 * Class     Category
8
 *
9
 * @package  Arcanesoft\Blog\Models
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Tag extends AbstractTaxonomy
13
{
14
    /* -----------------------------------------------------------------
15
     |  Constants
16
     | -----------------------------------------------------------------
17
     */
18
19
    const SELECT_CACHE_NAME = 'blog::tags.select-data';
20
21
    /* -----------------------------------------------------------------
22
     |  Properties
23
     | -----------------------------------------------------------------
24
     */
25
26
    /**
27
     * The database table used by the model
28
     *
29
     * @var string
30
     */
31
    protected $table = 'tags';
32
33
    /**
34
     * The event map for the model.
35
     *
36
     * @var array
37
     */
38
    protected $dispatchesEvents = [
39
        'creating'  => TagEvents\TagCreating::class,
40
        'created'   => TagEvents\TagCreated::class,
41
        'updating'  => TagEvents\TagUpdating::class,
42
        'updated'   => TagEvents\TagUpdated::class,
43
        'saving'    => TagEvents\TagSaving::class,
44
        'saved'     => TagEvents\TagSaved::class,
45
        'deleting'  => TagEvents\TagDeleting::class,
46
        'deleted'   => TagEvents\TagDeleted::class,
47
        'restoring' => TagEvents\TagRestoring::class,
48
        'restored'  => TagEvents\TagRestored::class,
49
    ];
50
51
    /* -----------------------------------------------------------------
52
     |  Relationships
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Posts relationship.
58
     *
59
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
60
     */
61 2
    public function posts()
62
    {
63 2
        return $this->belongsToMany(Post::class, $this->getPrefix().'post_tag');
64
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Main Methods
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Get the categories options for select input.
73
     *
74
     * @return \Illuminate\Support\Collection
75
     */
76 2
    public static function getSelectData()
77
    {
78 2
        $minutes = config('arcanesoft.blog.cache.tags.select-data', 5);
79
80
        /** @var  \Illuminate\Database\Eloquent\Collection  $categories */
81 2
        return cache()->remember(self::SELECT_CACHE_NAME, $minutes, function () {
82 2
            $withTranslations = Blog::instance()->isTranslatable();
83
84 2
            return self::all()->mapWithKeys(function (Tag $tag) use ($withTranslations) {
85
                return [
86 2
                    $tag->id => $withTranslations
87
                        ? implode(' / ', $tag->getTranslations('name'))
88 2
                        : $tag->name
89
                ];
90 2
            });
91
        })->toBase();
92
    }
93
}
94