Completed
Push — 2.4 ( d8c2c9...35279c )
by James
98:56 queued 97:01
created

Tag::findOrCreate()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 1
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
crap 4.0119
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Cachet.
5
 *
6
 * (c) Alt Three Services Limited
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CachetHQ\Cachet\Models;
13
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Support\Str;
16
17
class Tag extends Model
18
{
19
    /**
20
     * The attributes that should be casted to native types.
21
     *
22
     * @var string[]
23
     */
24
    protected $casts = [
25
        'name' => 'string',
26
    ];
27
28
    /**
29
     * The fillable properties.
30
     *
31
     * @var string[]
32
     */
33
    protected $fillable = ['name'];
34
35
    /**
36
     * Overrides the models boot method.
37
     *
38
     * @return void
39
     */
40 17
    public static function boot()
41
    {
42 17
        parent::boot();
43
44
        self::creating(function ($tag) {
45 3
            $tag->slug = Str::slug($tag->name);
46 17
        });
47
    }
48
49
    /**
50
     * Get the components relation.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
53
     */
54
    public function components()
55
    {
56
        return $this->belongsToMany(Component::class);
57
    }
58
59
    /**
60
     * @param array|\ArrayAccess $values
61
     *
62
     * @return \CachetHQ\Cachet\Models\Tag|static
63
     */
64 3
    public static function findOrCreate($values)
65
    {
66
        $tags = collect($values)->map(function ($value) {
67 3
            if ($value instanceof self) {
68
                return $value;
69
            }
70
71 3
            $tag = static::where('name', '=', $value)->first();
72
73 3
            if (!$tag instanceof self) {
74 3
                $tag = static::create([
0 ignored issues
show
Bug introduced by James Brooks
The method create() does not exist on CachetHQ\Cachet\Models\Tag. Did you maybe mean findOrCreate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
75 3
                    'name' => $value,
76 3
                    'slug' => Str::slug($value),
77
                ]);
78
            }
79
80 3
            return $tag;
81 3
        });
82
83 3
        return is_string($values) ? $tags->first() : $tags;
84
    }
85
}
86