Passed
Push — laravel-5 ( 66f120...6eb64b )
by Robert
02:51
created

src/Model/Tag.php (1 issue)

Severity
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 TagGroup group
17
 * @method static suggested()
18
 * @method static inGroup(string $groupName)
19
 */
20
class Tag extends Model
21
{
22
    protected $table = 'tagging_tags';
23
    public $timestamps = false;
24
    public $fillable = ['name'];
25
26
    /**
27
     * @param array $attributes
28
     */
29
    public function __construct(array $attributes = [])
30
    {
31
        parent::__construct($attributes);
32
33
        $this->connection = config('tagging.connection');
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function save(array $options = [])
40
    {
41
        if(strlen($this->name) < 1) {
42
            throw new \RuntimeException('Cannot save a tag with an empty name');
43
        }
44
45
        $this->slug = TaggingUtility::normalize($this->name);
46
47
        return parent::save($options);
48
    }
49
50
    /**
51
     * Tag group setter
52
     * @param string $groupName
53
     * @return Tag
54
     */
55
    public function setGroup($groupName)
56
    {
57
        $tagGroup = TagGroup::query()
58
            ->where('slug', TaggingUtility::normalize($groupName))
59
            ->first();
60
61
        if ($tagGroup) {
62
            $this->group()->associate($tagGroup);
63
            $this->save();
64
65
            return $this;
66
        } else {
67
            throw new \RuntimeException('No Tag Group found: '. $groupName);
68
        }
69
    }
70
71
    /**
72
     * Tag group remove
73
     * @param string $groupName
74
     * @return Tag
75
     */
76
    public function removeGroup(string $groupName)
77
    {
78
        $tagGroup = TagGroup::query()->where('slug', TaggingUtility::normalize($groupName))->first();
79
80
        if ($tagGroup) {
81
            $this->group()->dissociate($tagGroup);
0 ignored issues
show
The call to Illuminate\Database\Eloq...BelongsTo::dissociate() has too many arguments starting with $tagGroup. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            $this->group()->/** @scrutinizer ignore-call */ dissociate($tagGroup);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
82
            $this->save();
83
84
            return $this;
85
        } else {
86
            throw new \RuntimeException('No Tag Group found: '. $groupName);
87
        }
88
    }
89
90
    /**
91
     * Tag group helper function
92
     * @param string $groupName
93
     * @return bool
94
     */
95
    public function isInGroup($groupName): bool
96
    {
97
        if ($this->group && ($this->group->slug == TaggingUtility::normalize($groupName))) {
98
            return true;
99
        }
100
101
        return false;
102
    }
103
104
    /**
105
     * Tag group relationship
106
     */
107
    public function group()
108
    {
109
        return $this->belongsTo(TagGroup::class, 'tag_group_id');
110
    }
111
112
    /**
113
     * Get suggested tags
114
     */
115
    public function scopeSuggested($query)
116
    {
117
        return $query->where('suggest', true);
118
    }
119
120
    /**
121
     * Get suggested tags
122
     * @param Builder $query
123
     * @param $groupName
124
     * @return Builder
125
     */
126
    public function scopeInGroup(Builder $query, $groupName)
127
    {
128
        $groupSlug = TaggingUtility::normalize($groupName);
129
130
        return $query->whereHas('group', function (Builder $query) use ($groupSlug) {
131
            $query->where('slug', $groupSlug);
132
        });
133
    }
134
135
    /**
136
     * Set the name of the tag : $tag->name = 'myname';
137
     *
138
     * @param string $value
139
     */
140
    public function setNameAttribute(string $value)
141
    {
142
        $this->attributes['name'] = TaggingUtility::displayize($value);
143
    }
144
145
    /**
146
     * Look at the tags table and delete any tags that are no longer in use by any taggable database rows.
147
     * Does not delete tags where 'suggest' value is true
148
     *
149
     * @return mixed
150
     */
151
    public static function deleteUnused()
152
    {
153
        return (new static)->newQuery()
154
            ->where('count', '=', 0)
155
            ->where('suggest', false)
156
            ->delete();
157
    }
158
}
159