Tag::setGroup()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace Conner\Tagging\Model;
4
5
use Conner\Tagging\TaggingUtility;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
9
/**
10
 * @package Conner\Tagging\Model
11
 * @property string id
12
 * @property string name
13
 * @property string slug
14
 * @property bool suggest
15
 * @property integer count
16
 * @property integer tag_group_id
17
 * @property TagGroup group
18
 * @method static suggested()
19
 * @method static inGroup(string $group)
20
 */
21
class Tag extends Model
22
{
23
    protected $table = 'tagging_tags';
24
    public $timestamps = false;
25
    public $fillable = ['name'];
26
27
    /**
28
     * @param array $attributes
29
     */
30
    public function __construct(array $attributes = [])
31
    {
32
        parent::__construct($attributes);
33
34
        $this->connection = config('tagging.connection');
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function save(array $options = [])
41
    {
42
        if(strlen($this->name) < 1) {
43
            throw new \RuntimeException('Cannot save a tag with an empty name');
44
        }
45
46
        $this->slug = TaggingUtility::normalize($this->name);
47
48
        return parent::save($options);
49
    }
50
51
    /**
52
     * Tag group setter
53
     * @param string $group
54
     * @return Tag
55
     */
56
    public function setGroup(string $group)
57
    {
58
        $tagGroup = TagGroup::query()
59
            ->where('slug', TaggingUtility::normalize($group))
60
            ->first();
61
62
        if ($tagGroup) {
63
            $this->group()->associate($tagGroup);
64
            $this->save();
65
66
            return $this;
67
        } else {
68
            throw new \RuntimeException('No Tag Group found: '. $group);
69
        }
70
    }
71
72
    /**
73
     * Tag group remove
74
     * @return Tag
75
     */
76
    public function removeGroup()
77
    {
78
        $this->group()->dissociate();
79
        $this->save();
80
81
        return $this;
82
    }
83
84
    /**
85
     * Tag group helper function
86
     * @param string $groupName
87
     * @return bool
88
     */
89
    public function isInGroup($groupName): bool
90
    {
91
        if ($this->group && ($this->group->slug == TaggingUtility::normalize($groupName))) {
92
            return true;
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Tag group relationship
100
     */
101
    public function group()
102
    {
103
        return $this->belongsTo(TagGroup::class, 'tag_group_id');
104
    }
105
106
    /**
107
     * Get suggested tags
108
     */
109
    public function scopeSuggested($query)
110
    {
111
        return $query->where('suggest', true);
112
    }
113
114
    /**
115
     * Get suggested tags
116
     * @param Builder $query
117
     * @param $groupName
118
     * @return Builder
119
     */
120
    public function scopeInGroup(Builder $query, $groupName)
121
    {
122
        $groupSlug = TaggingUtility::normalize($groupName);
123
124
        return $query->whereHas('group', function (Builder $query) use ($groupSlug) {
125
            $query->where('slug', $groupSlug);
126
        });
127
    }
128
129
    /**
130
     * Set the name of the tag : $tag->name = 'myname';
131
     *
132
     * @param string $value
133
     */
134
    public function setNameAttribute(string $value)
135
    {
136
        $this->attributes['name'] = TaggingUtility::displayize($value);
137
    }
138
139
    /**
140
     * Look at the tags table and delete any tags that are no longer in use by any taggable database rows.
141
     * Does not delete tags where 'suggest' value is true
142
     *
143
     * @return mixed
144
     */
145
    public static function deleteUnused()
146
    {
147
        return (new static)->newQuery()
148
            ->where('count', '=', 0)
149
            ->where('suggest', false)
150
            ->delete();
151
    }
152
}
153